Tell us what’s happening:
Python Budget App, can’t get any chart tests to pass
I can’t understand why none of the create_spend_chart() tests are passing. It looks like it should to my eye, but even the title failing tells me I’m doing something wrong that I can’t understand. When I try using inspect on the browser, it gives hundreds of messages, and I have no idea how to filter through those in order to use them to help me.
Would appreciate any thoughts. Thanks!
Your code so far
Python Budget App, can’t get chart tests to pass
class Category:
def __init__(self, category):
self.category = category
self.total = 0
self.ledger = []
def __repr__(self):
title = f"{self.category:*^30}\n"
items = ""
total = 0
for item in self.ledger:
items += f"{item['description'][0:23]:23}" + f"{item['amount']:>7.2f}" + "\n"
total += item['amount']
output = title + items + "Total: " + str(total)
return output
def deposit(self, amount, description=""):
self.total += amount
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.total -= amount
self.ledger.append({"amount": -amount, "description": description})
return True
else:
return False
#balance in each account method
def get_balance(self):
return self.total
#transfer method
def transfer(self, amount, transfer_to):
if self.check_funds(amount):
#subtranct from self
self.total -= amount
self.ledger.append({"amount": -amount, "description": "Transfer to " + transfer_to.category})
#add to transferee
transfer_to.total += amount
transfer_to.ledger.append({"amount": amount, "description": "Transfer from " + self.category})
return True
else:
return False
##funds in each account method
def check_funds(self, amount):
if amount <= self.total:
return True
else:
return False
#see all withdrawals for each category method
def get_withdrawals(self):
items = ""
total = 0
for item in self.ledger:
if str(item['amount']).startswith("-"):
items += f"{str(item['amount'])}, "
total += item['amount']
trying = (str(total)[1:])
output = "Items " + items, "Total" + str(total)
return trying
categories=["Savings", "Car", "Rent", "Food"]
## create percentage array method
def get_percentages(categories=categories):
percentages = []
food_total = food.get_withdrawals()
car_total = car.get_withdrawals()
rent_total = rent.get_withdrawals()
savings_total = savings.get_withdrawals()
total_spend = 0
#total spend amount with if statements
if car_total == "":
total_spend += 0
else:
total_spend += float(car_total)
if food_total == "":
total_spend += 0
else:
total_spend += float(food_total)
if rent_total == "":
total_spend += 0
else:
total_spend += float(rent_total)
if savings_total == "":
savings_total = 0
else:
total_spend += float(savings_total)
##formats percent
food_per = str(round((float(food_total)/total_spend),2))[2:]
car_per = str(round((float(car_total)/total_spend),2))[2:]
rent_per = str(round((float(rent_total)/total_spend),2))[2:]
savings_per = str(round((float(savings_total)/total_spend),2))[2:]
#math for rounding percentages then append to array
get_savings = float(savings_per) if savings_per[1:] == 0 else float(savings_per) + (10-(float(savings_per[1:])))
percentages.append(get_savings)
get_car = float(car_per) if car_per[1:] == 0 else float(car_per)+(10-(float(car_per[1:])))
percentages.append(get_car)
get_rent = float(rent_per) if rent_per[1:] == 0 else float(rent_per)+(10-(float(rent_per[1:])))
percentages.append(get_rent)
get_food = float(food_per) if food_per[1:] == 0 else float(food_per)+(10-(float(food_per[1:])))
percentages.append(get_food)
return percentages
##start spend chart
def create_spend_chart(categories=categories):
percentages = get_percentages(categories)
title = "Percentage spent by category"
chart = '\n'
height = len(max(categories, key=len))
padded = [names.ljust(height) for names in categories]
for num in reversed(range(0, 110, 10)):
chart += f"{str(num) + '|':>4}"
for percent in percentages:
if percent >= num:
chart += " o "
else:
chart += " "
chart += " \n"
chart += " "+ "-" * 13 + "\n"
for name in zip(*padded):
chart += " " + (" ".join(name)) + " \n"
return title + chart.rstrip()
#print(savings_total, "hi")
food = Category("Food")
car = Category("Car")
rent = Category("Rent")
savings = Category("Savings")
savings.deposit(2000, "First check")
savings.transfer(200, food)
savings.transfer(350, car)
savings.transfer(1000, rent)
rent.withdraw(200, "january")
car.withdraw(200, "payment")
food.withdraw(75, "groceries")
food.withdraw(50, "yum")
rent.transfer(50, savings)
get_percentages()
create_spend_chart()
print(create_spend_chart())
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project