I have made the create_spend_chart function and it runs fine in my text editor and cmd prompt but it’s giving error every time I’m running it on repl.it
Below are my code and error description it’s giving me
Any help and suggestion will be great
Thanks in advance
def create_spend_chart(lcat):
total_with = 0
percent = list()
cname = list()
i = 100
ugraph = "Percentage spent by category\n"
lgraph = ''
for cat in lcat:
total_with += cat.get_withdrawl
for cat in lcat:
a = round((cat.get_withdrawl / total_with), 1)
percent.append(int(a*100))
cname.append(cat.categories)
while i >= 0:
bar = ' '
for per in percent:
if per >= i:
bar += 'o '
else:
bar += ' '
ugraph += str(i).rjust(3) + '|' + bar + '\n'
i -= 10
# ugraph = ugraph.rstrip()
dash = "-" + ("---" * len(percent))
dash = dash.rjust(len(dash)+4)
dash = dash.rstrip()
a = len(max(cname, key=len))
for i in range(a):
space = ' '
for cat in cname:
try:
space += cat[i] + ' '
except IndexError:
space += ' '
space += '\n'
lgraph += space
# lgraph = lgraph.rstrip()
return ugraph + dash + '\n' + lgraph
This test is a bit fustrating as you have to get the strings to mach exactly including white space. try # out the test module can then compare the output to the example and check the white space match. You might have to use rstrip for it inorder to work. Have a look at my code below and see if it helps.
def create_spend_chart(categories):
result = 'Percentage spent by category\n'
total = sum(x.spent for x in categories)
percentages = [(x.spent/total)//0.01 for x in categories]
for x in range(100, -10, -10):
result = result + str(x).rjust(3, " ") + '|'
for y in percentages:
if y >= x:
result = result + ' o '
else:
result = result + ' '
result = result + ' \n'
result = result + ' ' + '-'*len(percentages)*3 + '-\n'
maxLength = max(len(x.name) for x in categories)
for x in range(maxLength):
result = result + ' '
for y in categories:
if x < len(y.name):
result = result + ' ' + y.name[x] + ' '
else:
result = result + ' '
result = result + ' \n'
return result.rstrip() +' '
Thanks a lot for your reply and for helping out.
Your code runs fine at repl.it and is helpful to understand but I am still not getting what I’m doing wrong with my code.
Thanks for the code though.