Hi there,
I’ve nearly completed the Python Budget app certification problem, but I’m still struggling to pass the last test, related to the create_spend_chart function, and for the life of me I can’t figure out what I’m doing wrong. I’ve checked the formatting against another example on the forum and it seems to match character by character. I was also first confused about how to compute the percentage spent (the description is super vague about this), but several existing posts seem to confirm what I implemented. So I must be missing something, probably something banal, but someone please help me because I’m slowly going crazy.
Here’s the relevant part of mycode:
def get_total_expenditure(category):
expenditure = 0
for item in category.ledger:
if item['amount'] < 0:
expenditure += abs(item['amount'])
return expenditure
def create_spend_chart(categories):
# Initialize result
spend_chart = ''
# Compute spending in each category
spending = [get_total_expenditure(category) for category in categories]
total = sum(spending)
bar_height = [int((amount/total*100) / 10) for amount in spending]
# Determine some chart parameters
cat_width = 3
title = 'Percentage spent by category'
total_width = max(len(title),cat_width*len(categories) + 4)
# Print title
spend_chart += title + '\n'
# Print the bar part
for i in range(100,-1,-10):
spend_chart += str(i).rjust(3) + '|'
for j in bar_height:
if i <= j*10:
spend_chart += ' o '
else:
spend_chart += ' '
spend_chart += ' \n'
# Print the separation line
spend_chart += ' ' + '-'*(cat_width*len(categories) + 1) + '\n'
# Print the categories
max_name_length = max([len(cat.name) for cat in categories])
expanded_cat_names = [cat.name.ljust(max_name_length) for cat in categories]
for i in range(max_name_length):
spend_chart += ' '
for j in range(len(categories)):
spend_chart += ' ' + expanded_cat_names[j][i] + ' '
if i < max_name_length - 1:
spend_chart += ' \n'
# Return result
return spend_chart.strip()
If someone could help me figure out what I’m missing I would be so grateful!