Tell us what’s happening:
The percentage calculation doesn’t pass, but it looks correct. The broken lines between the bar and the labelling keep shifting depending on the number of categories in the list parameters.
The chaart doesn’t pass
Your code so far
import math
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
self.total_withdrawal = 0
def deposit(self, amount, description = ""):
self.balance = self.balance + amount
self.ledger.append({'amount':amount, 'description':description})
def withdraw(self, amount, description = ""):
if self.check_funds(amount):
self.balance = self.balance - amount
self.ledger.append({'amount':-amount, 'description':description})
self.total_withdrawal = self.total_withdrawal + amount
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def __str__(self):
display = f"{self.name.center(30, '*')}\n"
for i in self.ledger:
display = display + f"{i['description'][:23]:<23}{i['amount']:>7.2f}\n"
display = display + f"Total: {self.balance:.2f}"
return display
def check_funds(self, amount):
if amount > self.balance:
return False
else:
return True
def create_spend_chart(categories):
display = "Percentage spent by category\n"
percentages = []
total_spent = 0
category_names =[]
for category in categories:
category_names.append(category.name)
total_spent += category.total_withdrawal
for category in categories:
percentages.append((math.floor(((category.total_withdrawal/total_spent)*100)/10 )* 10))
for i in range(100,-10,-10):
display += f"\n{i:>3}| "
for percent in percentages:
if percent >= i:
display += 'o '
else:
display += ' '
display += f"\n {'-'* (3*len(percentages)+1):>10}"
for i in range(len(max(category_names, key = len))):
display += f"\n{' ':>4}"
for name in category_names:
if i < len(name):
display += f" {name[i]} "
else:
display += " "
return display
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')
food.transfer(50, clothing)
entertainment = Category("Entertainment")
entertainment.deposit(500, "initial deposit")
entertainment.withdraw(50, "movie")
entertainment.withdraw(30, "music")
entertainment.withdraw(20, "game")
print(food)
print(create_spend_chart([food,entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36 Edg/154.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App