Tell us what’s happening:
I have re-wrote the code twice and tried different approaches, but test 20,23,24. Please help me.
Each line in create_spend_chart chart should have the same length. Bars for different categories should be separated by two spaces, with additional two spaces after the final bar.
create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length,
create_spend_chart should print a different chart representation. Check that
Your code so far
class Category:
def __init__(self,name):
self.name=name
self.ledger=[]
def deposit(self,amount,description=""):
self.ledger.append({"amount":amount,"description":description})
def withdraw(self,amount,description=""):
if self.check_funds(amount):
self.ledger.append({"amount":round(-amount,2),"description":description})
return True
else:
return False
def get_balance(self):
balance=sum([entry["amount"] for entry in self.ledger])
return 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 check_funds(self,amount):
return True if self.get_balance()>=amount else False
def __str__(self):
output=""
name_len=len(self.name)
output+=f"{'*'*((30-name_len)//2)}{self.name}{'*'*((30-name_len)//2)}"
for entry in self.ledger:
description=entry['description']
amount=entry['amount']
output+=f"\n{description[:23]}"
output+=" "*(30-len(description[:23])-len(f"{amount:.2f}"))
output+=f"{entry['amount']:.2f}"[:7]
output+=f"\nTotal: {self.get_balance()}"
return output
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(50)
print(food)
shit=Category("SHIT")
def create_spend_chart(categories):
output=""
output+="Percentage spent by category"
category_vise_spending_dictionary={}
total_spending=sum([sum([entry['amount'] for entry in category.ledger if entry['amount']<=0 ])for category in categories])
len_of_name=[]
for category in categories:
spending=sum([entry['amount'] for entry in category.ledger if entry['amount']<=0 ])
category_vise_spending_dictionary[category.name]=((spending/total_spending)*100)//10*10
len_of_name.append(len(category.name))
#print(category_vise_spending_dictionary)
for y_label in range(100,-1,-10):
output+=f"\n{' '*(3-len(str(y_label)))}{y_label}|"
for category in category_vise_spending_dictionary:
if y_label<=category_vise_spending_dictionary[category]:
output+=" o "
else:
output+=" "
output+='\n -'
output+="---"*len(categories)
output+="\n "
for index in range(max(len_of_name)):
for category in category_vise_spending_dictionary:
try:
output+=f" {category[index]} "
except:
output+=" "
output+="\n "
return output
chart=create_spend_chart([clothing,food,shit])
#for line in chart.split("\n"):
# print(len(line))
print(chart)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App
