Tell us what’s happening:
I don’t know why this code is not passing. Am I missing something?
Your code so far
class Category:
withdrows_amount = 0
def __init__(self, category):
self.ledger = []
self.category = category
self.balance = 0
self.withdrown = 0
def __str__(self):
cat = self.category
cat = cat.center(30, '*')
bal = format(self.balance, '.2f')
list = []
for item in self.ledger:
g = format(item['amount'], '.2f')
j = item['description']
if len(j) <= 23:
dif = 30 - len(j)
else: dif = 7
strg = ( f'{j[:23]}{str(g).rjust(dif)}\n')
list.append(strg)
list_str = f'{cat}\n{"".join(list)}Total: {bal}'
return list_str
# depositing
def deposit(self, amount, description =''):
self.ledger.append({'amount': amount, 'description': description})
self.balance += amount
def withdraw(self, amount, description =''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.balance -= amount
self.withdrown += amount
Category.withdrows_amount += amount
return True
else: return False
def transfer(self, amount, category):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f'transfer to {category.category}'})
category.ledger.append({'amount': amount, 'description': f'transfer from {self.category}'})
self.balance -= amount
category.balance += amount
return True
else: return False
def get_balance(self):
return self.balance
def check_funds(self, amount):
if amount > self.balance:
return False
else: return True
food = Category('Food')
auto = Category('Auto')
clothing = Category('Clothing')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50, clothing)
clothing.withdraw(15.89, 'sheos')
auto.deposit(1000, "deposit")
auto.withdraw(100, "groceries")
print(clothing)
print(food)
print(food.withdrown)
print(Category.withdrows_amount)
print(auto)
def create_spend_chart(categories):
percentage_list = ['100|',' 90|',' 80|', ' 70|', ' 60|', ' 50|', ' 40|', ' 30|', ' 20|', ' 10|', ' 0|']
point_list = []
dashes = '-'
space = ' '
# getting the amountthat was spent and turning it to percentges
for cat in categories:
cat.category_percentage = round(((cat.withdrown / Category.withdrows_amount)*10))
# calculating o's length
for category in categories:
i = category.category_percentage
points = ((10 - i)* space) + 'o' + i * 'o'
point_list.append(points)
# calculating dashes length
for i in categories:
dash = '---'
dashes += dash
# mesurig the longest item
max_leng = 0
for i in categories:
i = (i.category)
if len(str(i))> max_leng:
max_leng = len(str(i))
# making all the items identical in length
for idx, item in enumerate(categories):
# make sure max length is measuring just the category name not the whole callable string
item = item.category
if len(str(item)) < max_leng:
dif = max_leng - len(str(item))
dif = dif*' '
categories[idx] = str(item) + dif
# make sure other items in the list are also different from the callable string so they dont call the whole list
else: categories[idx] = str(item) + ' '
# printing the o's
print('Percentage spent by category')
for i in range(11):
print(percentage_list[i], end=' ')
for point in point_list:
print(point[i], end=' ')
print()
# printing the dashes
print(' ',dashes)
# printing the categories
for i in range((max_leng)):
print(' ', end= '')
for cat in categories:
print(str(cat)[i], end=' ')
print()
create_spend_chart([food, clothing, auto])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project