Tell us what’s happening:
I can’t figure out the spacing for the last test. I believe I have everything generating correctly, but I just can’t get my output to pass. It’s not pretty but it is working.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
def __str__(self):
category_str = ''
#add the name to the string
half_length = len(self.name) // 2
number_of_stars = 15 - half_length
for _ in range(number_of_stars):
category_str += '*'
category_str += self.name
for _ in range(number_of_stars):
category_str += '*'
category_str += '\n'
#add ledger items
for entry in self.ledger:
amount, description = entry.values()
#pad the description
if len(description) < 23:
number_of_spaces = 23 - len(description)
for _ in range(number_of_spaces):
description += ' '
else:
description = description[:23]
#pad the amount
amount_str = str(amount)
if amount_str.find('.') == -1:
amount_str += '.00'
if len(amount_str) < 7:
for _ in range(7 - len(amount_str)):
amount_str = ' ' + amount_str
else:
amount_str = amount_str[:7]
category_str += description + amount_str + '\n'
#get the total line
category_str += f'Total: {self.balance}'
return category_str
def deposit(self, amount, description = ''):
self.ledger.append({'amount': amount, 'description': description})
self.balance += amount
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({'amount': amount * -1, 'description': description})
self.balance -= amount
return True
return False
def get_balance(self):
return self.balance
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {other_category.name}')
other_category.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self, amount):
if amount <= self.balance:
return True
return False
def create_spend_chart(categories):
bar_chart = ''
percentages = {}
for category in categories:
percentages[category.name] = round(sum([entry['amount'] for entry in category.ledger if entry['amount'] < 0]), 2) * -1
sum_of_withdraws = sum(percentages.values())
for category, value in percentages.items():
percentages[category] = round((percentages[category] / sum_of_withdraws) * 100, 2)
#print(percentages)
#create strings for output
header = 'Percentage spent by category\n'
str_100 = '100|'
str_90 = ' 90|'
str_80 = ' 80|'
str_70 = ' 70|'
str_60 = ' 60|'
str_50 = ' 50|'
str_40 = ' 40|'
str_30 = ' 30|'
str_20 = ' 20|'
str_10 = ' 10|'
str_0 = ' 0|'
for value in percentages.values():
if value > 100:
str_100 += ' o '
else:
str_100 += ' '
if value > 90:
str_90 += ' o '
else:
str_90 += ' '
if value > 80:
str_80 += ' o '
else:
str_80 += ' '
if value > 70:
str_70 += ' o '
else:
str_70 += ' '
if value > 60:
str_60 += ' o '
else:
str_60 += ' '
if value > 50:
str_50 += ' o '
else:
str_50 += ' '
if value > 40:
str_40 += ' o '
else:
str_40 += ' '
if value > 30:
str_30 += ' o '
else:
str_30 += ' '
if value > 20:
str_20 += ' o '
else:
str_20 += ' '
if value > 10:
str_10 += ' o '
else:
str_10 += ' '
if value >= 0:
str_0 += ' o '
else:
str_0 += ' '
str_100 += ' \n'
str_90 += ' \n'
str_80 += ' \n'
str_70 += ' \n'
str_60 += ' \n'
str_50 += ' \n'
str_40 += ' \n'
str_30 += ' \n'
str_20 += ' \n'
str_10 += ' \n'
str_0 += ' \n'
bottom_bar = ' '
for _ in range(len(str_0) - 5):
bottom_bar += '-'
bottom_bar += '\n'
vertical_names_str = ''
longest_name_length = max([len(category.name) for category in categories])
#print(longest_name_length)
for i in range(longest_name_length):
vertical_names_str += ' '
for category in categories:
if i < len(category.name):
vertical_names_str += ' ' + category.name[i] + ' '
else:
vertical_names_str += ' '
vertical_names_str += ' \n'
#combine everything, print it, return it
bar_chart = header + str_100 + str_90 + str_80 + str_70 + str_60 + str_50 + str_40 + str_30 + str_20 + str_10 + str_0 + bottom_bar + vertical_names_str
print(bar_chart)
return bar_chart
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project