I am getting below errors in Assertion test-budget app. I am printing lines length as part of output for checking.
// running tests
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
20. 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.
23. create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces after the final category.
24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.
// tests completed
***Food***
initial deposit 1000.00
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50.00
Transfer to Auto -50.00
Total: 873.96
Line 0: length = 14
Line 1: length = 14
Line 2: length = 14
Line 3: length = 14
Line 4: length = 14
Line 5: length = 14
Line 6: length = 14
Line 7: length = 14
Line 8: length = 14
Line 9: length = 14
Line 10: length = 14
Line 11: length = 14
Line 12: length = 14
Line 13: length = 14
Line 14: length = 14
Line 15: length = 14
Line 16: length = 14
Line 17: length = 14
Line 18: length = 14
Line 19: length = 14
Title: length = 29
Percentage spent by category
100|
90|
80|
70|
60|o
50|o
40|o
30|o
20|o
10|o o o
0|o o o
----------
F C A
o l u
o o t
d t o
h
i
n
g
Code:
import math
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': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
return sum(ledgers\["amount"\] for ledgers in self.ledger)
def transfer(self,amount,Destination):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f"Transfer to {Destination.name}"})
Destination.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
return True
else:
return False
def check_funds(self,amount):
if self.get_balance()<amount:
return False
else:
return True
def **str**(self):
res=""
for item in self.ledger:
res+=f"{item\['description'\]\[:23\]:<23}{item\['amount'\]:>7.2f}\\n"
res.rstrip("\\n")
balance=self.get_balance()
res+=f"Total: {balance:.2f}"
return(f"{self.name:\*^30}\\n{res}")
def create_spend_chart(categories):
Total_spend=0
for item in categories:
for ledger in item.ledger:
if ledger\['amount'\]<0:
Total_spend+=-(ledger\['amount'\])
Cat_spend=dict()
for item in categories:
spend=0
for ledger in item.ledger:
if ledger\['amount'\]<0:
spend+=-(ledger\['amount'\])
Cat_spend\[item.name\]=spend
Cat_per_spend=dict()
for item in categories:
Cat_per_spend\[item.name\]=((Cat_spend\[item.name\]/Total_spend)\*100)
#rounding down percentage to nearest 10
Cat_per_spend\[item.name\]=math.floor((Cat_per_spend\[item.name\]//10)\*10)
line=\[''\]\*12
title="Percentage spent by category\\n"
a=list(range(100,-10,-10))
for i in range(0,11):
line\[i\]=f"{a\[i\]:>3}|"
for x in Cat_per_spend.values():
for i in range(10,-1,-1):
if int(line\[i\]\[0:3\].strip())<=x:
line\[i\]+="o "+" "
#x-=10
else:
line\[i\]+=" "
for i in range(0,11):
line\[i\]+=" "
line\[11\]+=" -"
keys=list(Cat_per_spend.keys())
for i in categories:
line\[11\]+="-"\*3
lth=len(max(keys,key=len))
line+=\[" "\]*lth
print(line\[12\])
for key in keys:
key=' '+key+' '*(lth-len(key))
for b in range(1,lth+1):
line\[11+b\]+=key\[b\]+" "
for i, lines in enumerate(line):
print(f"Line {i}: length = {len(lines)}")
print(f"Title: length = {len(title)}")
res=""
res=res+title
for lines in line:
lines=lines.rstrip(" ")
res+='\\n'+lines
res=res.replace('\\n','',1)
return res
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)
clothing.withdraw(30,"Dress")
auto=Category("Auto")
food.transfer(50,auto)
print(food)
auto.withdraw(30,"travel")
#print(clothing)
listCat=\[food,clothing,auto\]
print(create_spend_chart(listCat))
please post your code properly formatted, we are not able to debug the code as it is posted as it has lost all indentation
There are two ways you can format your code to make it easier to read and test:
After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)
Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.
To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:
import math
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': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
return sum(ledgers["amount"] for ledgers in self.ledger)
def transfer(self,amount,Destination):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f"Transfer to {Destination.name}"})
Destination.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
return True
else:
return False
def check_funds(self,amount):
if self.get_balance()<amount:
return False
else:
return True
def __str__(self):
res=""
for item in self.ledger:
res+=f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
res.rstrip("\n")
balance=self.get_balance()
res+=f"Total: {balance:.2f}"
return(f"{self.name:*^30}\n{res}")
def create_spend_chart(categories):
Total_spend=0
for item in categories:
for ledger in item.ledger:
if ledger['amount']<0:
Total_spend+=-(ledger['amount'])
Cat_spend=dict()
for item in categories:
spend=0
for ledger in item.ledger:
if ledger['amount']<0:
spend+=-(ledger['amount'])
Cat_spend[item.name]=spend
Cat_per_spend=dict()
for item in categories:
Cat_per_spend[item.name]=((Cat_spend[item.name]/Total_spend)*100)
#rounding down percentage to nearest 10
Cat_per_spend[item.name]=math.floor((Cat_per_spend[item.name]//10)*10)
line=['']*12
title="Percentage spent by category\n"
a=list(range(100,-10,-10))
for i in range(0,11):
line[i]=f"{a[i]:>3}|"
for x in Cat_per_spend.values():
for i in range(10,-1,-1):
if int(line[i][0:3].strip())<=x:
line[i]+="o "+" "
#x-=10
else:
line[i]+=" "
for i in range(0,11):
line[i]+=" "
line[11]+=" -"
keys=list(Cat_per_spend.keys())
for i in categories:
line[11]+="-"*3
lth=len(max(keys,key=len))
line+=[" "]*lth
print(line[12])
for key in keys:
key=' '+key+' '*(lth-len(key))
for b in range(1,lth+1):
line[11+b]+=key[b]+" "
#for i, lines in enumerate(line):
#print(f"Line {i}: length = {len(lines)}")
#print(f"Title: length = {len(title)}")
res=""
res=res+title
for lines in line:
lines=lines.rstrip(" ")
res+='\n'+lines
res=res.replace('\n','',1)
return res
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)
clothing.withdraw(30,"Dress")
auto=Category("Auto")
food.transfer(50,auto)
print(food)
auto.withdraw(30,"travel")
listCat=[food,clothing,auto]
print(create_spend_chart(listCat))
Hi, Please find updated code, I have added spaces but still getting error in 19,20,23,24 assertion tests
import math
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': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
return sum(ledgers["amount"] for ledgers in self.ledger)
def transfer(self,amount,Destination):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f"Transfer to {Destination.name}"})
Destination.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
return True
else:
return False
def check_funds(self,amount):
if self.get_balance()<amount:
return False
else:
return True
def __str__(self):
res=""
for item in self.ledger:
res+=f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
res.rstrip("\n")
balance=self.get_balance()
res+=f"Total: {balance:.2f}"
return(f"{self.name:*^30}\n{res}")
def create_spend_chart(categories):
Total_spend=0
for item in categories:
for ledger in item.ledger:
if ledger['amount']<0:
Total_spend+=-(ledger['amount'])
Cat_spend=dict()
for item in categories:
spend=0
for ledger in item.ledger:
if ledger['amount']<0:
spend+=-(ledger['amount'])
Cat_spend[item.name]=spend
Cat_per_spend=dict()
for item in categories:
Cat_per_spend[item.name]=((Cat_spend[item.name]/Total_spend)*100)
#rounding down percentage to nearest 10
Cat_per_spend[item.name]=math.floor((Cat_per_spend[item.name]//10)*10)
line=['']*12
title="Percentage spent by category\n"
a=list(range(100,-10,-10))
for i in range(0,11):
line[i]=f"{a[i]:>3}| "
for x in Cat_per_spend.values():
for i in range(10,-1,-1):
if int(line[i][0:3].strip())<=x:
line[i]+="o "+" "
#x-=10
else:
line[i]+=" "
for i in range(0,11):
line[i]+=" "
line[11]+=" -"
keys=list(Cat_per_spend.keys())
for i in categories:
line[11]+="-"*3
lth=len(max(keys,key=len))
line+=[" "]*lth
line[12]=" "
for key in keys:
key=' '+key+' '*(lth-len(key))
for b in range(1,lth+1):
line[11+b]+=key[b]+" "
#for i, lines in enumerate(line):
#print(f"Line {i}: length = {len(lines)}")
#print(f"Title: length = {len(title)}")
res=""
res=res+title
for lines in line:
lines=lines.rstrip(" ")
res+='\n'+lines
res=res.replace('\n','',1)
return res
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)
clothing.withdraw(30,"Dress")
auto=Category("Auto")
food.transfer(50,auto)
print(food)
auto.withdraw(30,"travel")
#print(clothing)
listCat=[food,clothing,auto]
print(create_spend_chart(listCat))
#print(create_spend_chart(listCat).replace(" ","."))