Tell us what’s happening:
Eventhough im getting the solution in terminal, i cannot pass few of the steps. I have tried to solve this problem in a different approach and thus, i cannot find the solution from other posts. I see assertion error in console, i would appreciate a hint on how to go solve this.
Your code so far
import math
import itertools
class Category:
def __init__(self,name):
self.name=name
self.ledger=[]
self.balance=0
def deposit(self,amount,description=""):
self.balance+=amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self,amount,description=""):
if self.check_funds(amount):
self.balance-=amount
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self,amount,other):
if self.check_funds(amount):
self.balance-=amount
self.ledger.append({'amount': -amount, 'description': f"Transfer to {other.name}"})
other.balance+=amount
other.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
return True
else:
return False
def check_funds(self,amount):
if amount>self.balance:
return False
else:
return True
def __str__(self):
dot='*'
ends=round((30-len(self.name))/2)*dot
title = f"{ends}{self.name}{ends}\n"
for x in self.ledger:
des=x["description"][0:23]
val_1=x["amount"]
val="{:.2f}".format(val_1)
title+= f"{des:<23}{val:>7}\n"
Final=f"Total: {self.balance}"
title += Final
return title
def create_spend_chart(categories):
chart_title=["Percentage spent by category"]
sum_all=0
for a in categories:
for x in a.ledger:
val=x["amount"]
if val<0:
sum_all+=val
per_cat=[]
for a in categories:
sum_a=0
for x in a.ledger:
val=x["amount"]
if val<0:
sum_a+=val
div=10*(math.floor((sum_a*100/sum_all)/10))
per_cat.append(div)
b=" "
c="-"
d="o"
maxlen = max([len(w.name) for w in categories])
y_axis=[f"{i:>4}|" for i in range(100,-1,-10)]
for i in range(maxlen+1):
y_axis.append(f"{b*5}")
k=[11*b+c]
for n in range(len(per_cat)):
s=""
s+=(10-int(per_cat[n]/10))*b + (int(per_cat[n]/10)+1)*d + c + categories[n].name
k.append(s)
k.append(11*b+c)
k.append(11*b+c)
for y, x in itertools.zip_longest(y_axis,itertools.zip_longest(*k, fillvalue=" "), fillvalue=" "):
row="".join(x)
chart_title.append(f'{y}{row}')
return "\n".join(chart_title)
food=Category("food")
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
print(food.get_balance())
print(str(food))
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)
print(food)
# Food Category
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(620, 'groceries')
print(food)
#Clothing category
clothing = Category('Clothing')
food.transfer(1, clothing)
clothing.deposit(1000, 'initial deposit')
clothing.withdraw(204, 'restaurant and more food for dessert')
print(clothing)
#Auto category
auto = Category('Auto')
clothing.transfer(1, auto)
auto.deposit(100000, 'initial deposit')
auto.withdraw(124, 'groceries')
auto.transfer(1, food)
stuff_list = [food, clothing, auto]
print(auto)
print(create_spend_chart(stuff_list))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App