Tell us what’s happening:
I am trying to finish the budget app (3rd Python Project). The code is a little bit mess (sorry) but runs without any problems (i think so). Even though the result seems to be similar to the solution i can’t pass the final test. It says “Expected different chart representation. Check that all spacing is exact.”. Also the the create_spend_chart function in the main.py runs without any errors but the test fails because there is a difference in spacing … What? How? Help?
Thank you
Your code so far
def create_spend_chart(categories):
##Function to round down given numbers (n)
def truncate(n, decimals=0):
multiplier = 10 ** decimals
return int(n * multiplier) / multiplier
spend = list()
percent = list()
cat_names = list()
number_of_items = len(categories)
longest = 0
chart = ""
##Find the total amount spent for each category and add it to a list called spend
##Also add each category name to another list called cat_names
for cat in categories:
total = 0
for l in cat.ledger:
if (l["amount"] < 0):
total += l["amount"]
spend.append(-total)
cat_names.append(cat.name)
##Find the Percentage spend by each category and add it to a list called percent
for s in spend:
percent.append(100 * truncate(s / sum(spend), 1))
chart = "Percentage spent by category"
####Create the chart####
for r in range(100, -1, -10):
##Create the numbers from 100 to 0
chart += f"\n{r:>3}| "
##Create the bars for each category
for n in range(number_of_items):
if (percent[n] >= r):
chart += "o"
else:
chart += " "
chart += " "
##Add -
chart += "\n" + (4 * " ") + (3 * "-" * number_of_items) + "-"
##Find the longest name
for c in cat_names:
if (len(c) > longest):
longest = len(c)
##Add the category names
for i in range(longest):
chart += "\n" + (5 * " ")
for j in range(len(cat_names)):
try:
chart += cat_names[j][i]
chart += " "
except:
chart += 3 * " "
print(chart)
Challenge: Budget App
Link to the challenge: