Tell us what’s happening:
I can’t seem to get Step 19 to pass. The output looks good, the percentages are rounded down to the nearest 10. Can anyone help out? Thanks!
Your code so far
total_spent = 0 ;
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.spent = 0
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=''):
global total_spent
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.spent += amount
total_spent += amount
return(True)
else:
return(False)
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction['amount']
return(balance)
def transfer(self, amount, other_cat):
if self.check_funds(amount) == True:
self.ledger.append({'amount': -amount, 'description': f"Transfer to {other_cat.name}"})
other_cat.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
return(True)
else:
return(False)
def check_funds(self, amount):
if amount <= self.get_balance():
return(True)
else:
return(False)
def __str__(self):
self.stars_left = round((30 - len(self.name)) / 2) ;
self.stars_right = round((30 - len(self.name)) / 2) ;
self.title_line = '*' * self.stars_left + self.name + '*' * self.stars_right + '\n'
ledger_contents = ''
for transaction in self.ledger:
if "." in str(transaction['amount']):
amount = str(transaction['amount'])
else:
amount = str(transaction['amount']) + '.00'
if len(transaction['description']) >= 23:
description = transaction['description'][:23]
spaces = 7 - len(amount)
else:
description = transaction['description']
spaces = 30 - len(description) - len(amount)
ledger_contents += description + " " * spaces + amount + "\n"
return self.title_line + ledger_contents + "Total: " + str(self.get_balance())
def create_spend_chart(categories):
lines = 'Percentage spent by category\n'
nb_cat = len(categories)
for i in range(100,-10,-10):
current_line = str(i).rjust(3) + "|"
for cat in categories:
perc = 100 * cat.spent / total_spent
perc = int(perc/10) * 10
if i == 0:
current_line += " o "
elif (perc > 0 and perc % i == 0) or perc >=i:
current_line += " o "
else:
current_line += " "
lines += current_line + '\n'
lines += " " + "_"* nb_cat * 3 + "_\n"
max_cat_length = 0
for cat in categories:
if len(cat.name) > max_cat_length:
max_cat_length = len(cat.name)
for i in range(0, max_cat_length):
current_line = ' '
for cat in categories:
if len(cat.name) >= i+1 :
current_line += ' ' + cat.name[i] + ' '
else:
current_line += ' '
lines += current_line + '\n'
return lines
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(300, 'pay day')
clothing.withdraw(158.39, "new outfit")
food.transfer(50, clothing)
print(food)
print('')
print(create_spend_chart([food, clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App