Tell us what’s happening:
In the create_spend_chart test, i’m getting an error. Everything is the same as expected except that the business category will have 0% spending according to the test whereas i’m getting 10%. Now going through the test_module.py, i found out that for that particular test total spending was 149.94. The withdrawal from business category was 10.99 which amounts to 7.33% rounded to 10%. So i don’t understand why is the test module using 0% as the expected output string. Only 1 test is failing and this is it. Please help me understand how can it be 0% instead of 10%.
Your code so far
class Category:
''' Objects of different expenditure categories belong to this class '''
def __init__(self, category):
self.category=category
self.ledger=[]
self.balance=0
def deposit(self, amount, description=None):
if description is None:
description=''
self.ledger.append({"amount" : amount, "description" : description})
self.balance+=amount
def withdraw(self, amount, description=None):
if description is None:
description=''
if self.check_funds(amount):
self.ledger.append({"amount" : -amount, "description" : description})
self.balance-=amount
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, other):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {other.category}")
other.deposit(amount, f"Transfer from {self.category}")
return True
else:
return False
def check_funds(self, amount):
if self.balance>=amount:
return True
else:
return False
def __str__(self):
string=''
space=30-len(self.category)
string+="*"*(space//2)+self.category+"*"*(space//2)
if len(self.category)%2==0:
string+='\n'
else:
string+='*\n'
for i in range(len(self.ledger)):
string+=(self.ledger[i]['description']+" "*30)[:23]
if isinstance(self.ledger[i]['amount'],int):
spaces=4-len(str(self.ledger[i]['amount']))
string+=' '*spaces+str(self.ledger[i]['amount'])+'.00'
else:
spaces=7-len(str(self.ledger[i]['amount']))
string+=' '*spaces+str(self.ledger[i]['amount'])
string+='\n'
string+='Total: '+str(self.balance)
return string
def create_spend_chart(categories):
#lets calculate total withdrawl across categories and also each withdrawl
wd=[]
total=0
for i in range(len(categories)):
spending=0
for item in categories[i].ledger:
if item['amount']<0:
spending+=abs(item['amount'])
wd.append(spending)
total+=spending
#now find percentage in each case to nearest 10
for i in range(len(categories)):
wd[i]=(wd[i]/total)*100
wd[i]=(wd[i]-(wd[i]%10)) if wd[i]%10<5.0 else (wd[i]+(10-wd[i]%10))
#print(wd)
#Now comes the plotting part
string='Percentage Spent by category\n'
for i in range(100,-10,-10):
if i==0:
string+= ' '+str(i)+'|'
elif i<100:
string+=' '+str(i)+'|'
else:
string+=str(i)+'|'
for j in range(len(categories)):
string+=" "+("o" if wd[j]>=i else " ")+" "
string+='\n'
string+=' '*4+'-'*3*len(categories)+'\n'
#return string
name=[item.category for item in categories]
#print(name[0][2])
for i in range(len(max(name, key=len))):
string+=' '*4
for j in range(len(categories)):
string+=' '+(name[j][i] if i<len(name[j]) else ' ')+' '
string+='\n'
return string
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Budget App
Link to the challenge: