Tell us what’s happening:
Test #19 fails: The rounding is not passing even though the output on the graph seems correct as are the returned percentages in the output. Tests 20, 23 and 24 also fail, but that is a different issue, probably related to spacing, which I will look into in greater detail on my own before asking for help on that.
Output
Your code so far
def create_spend_chart(categories):
bar_chart_str = 'Percentage spent by category\n'
total_withdrawals = 0
# [{category.name: category_withdrawal},...]
category_withdrawals = []
for category in categories:
category_withdrawal = 0
for ledger_dict in category.ledger:
if ledger_dict['amount'] < 0:
total_withdrawals -= ledger_dict['amount']
category_withdrawal -= ledger_dict['amount']
category_withdrawals.append({category.name: category_withdrawal})
category_withdrawals = sorted(category_withdrawals,
key=lambda d: list(d.values())[0],
reverse=True)
category_names = [list(d.keys())[0] for d in category_withdrawals]
print(category_names)
category_amounts = [list(d.values())[0] for d in category_withdrawals]
print(category_amounts)
category_pcts = [math.floor(10*num/total_withdrawals)*10 for num in category_amounts]
print(category_pcts)
for i in range(100, -10, -10):
y_index = (str(i)+'|').rjust(4)
bar_chart_str += y_index
for pct in category_pcts:
if i <= pct:
bar_chart_str += ' o '
bar_chart_str += '\n'
num_categories = len(category_names)
x_axis = ' '*4
for i in range(num_categories):
x_axis += '---'
x_axis += '-'
bar_chart_str += x_axis + '\n'
left_pad = ' '*4
longest_cat_name = 0
for name in category_names:
if len(name) > longest_cat_name:
longest_cat_name = len(name)
category_letters = [list(name) for name in category_names]
#print(category_letters)
for row in range(longest_cat_name):
bar_chart_str += left_pad
for col in range(num_categories):
len_current_cat = len(category_letters[col])
if row < len_current_cat:
bar_chart_str += f' {category_letters[col][row]} '
if col == len(category_letters) - 0:
bar_chart_str += ' '
bar_chart_str += '\n'
bar_chart_str = bar_chart_str.rstrip('\n')
return bar_chart_str
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')
auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')
categories = [food, clothing, auto]
print(create_spend_chart(categories))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App
