Tell us what’s happening:
My codes have past all the tests except the very last one:
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.
I am not familiar with reading the tests from the browser console. It would be greatly appreciated if anyone could point out what is wrong.
Your code so far
class Category:
def __init__(self, cat):
self.name = cat
self.ledger = []
self.spending = 0
def __str__(self):
output = ''
output += f'{self.name.center(30, "*")}\n'
for item in self.ledger:
output += f"{item['description'][:23]:<23}"
output += f"{item['amount']:>7.2f}\n"
output += f'Total: {self.get_balance()}'
return output
def deposit(self, amount, description = ''):
self.ledger.append({'amount': amount, 'description': description})
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item['amount']
return balance
def check_funds(self, amount):
return False if amount > self.get_balance() else True
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({'amount': - amount, 'description': description})
self.spending += amount
return True
else:
return False
def transfer(self, amount, cat):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {cat.name}')
cat.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def create_spend_chart(categories):
total = 0
height = {}
name_lens = []
for cat in categories:
total += cat.spending
name_lens.append(len(cat.name))
for cat in categories:
height[cat.name] = (((cat.spending / total)*100)//10)*10
num_lines = []
for i in range(11):
num_lines.append(f'{i*10:>3}|')
axis = ' '
for cat in categories:
for i in range(11):
num_lines[i] += ' o ' if height[cat.name] >= i else ' '
axis += '---'
if cat == categories[-1]:
for i in range(11):
num_lines[i] += ' \n'
axis += '-\n'
reversed_lines = num_lines[::-1]
chart = 'Percentage spent by category\n'
for line in reversed_lines:
chart += line
chart += axis
max_len = max(name_lens)
name_lines = []
for i in range(max_len):
name_lines.append(' ')
for i in range(max_len):
for cat in categories:
if i < len(cat.name):
name_lines[i] += f' {cat.name[i]} '
else:
name_lines[i] += ' '
if cat == categories[-1]:
if i != max_len - 1:
name_lines[i] += ' \n'
else:
name_lines[i] += ' '
chart += name_lines[i]
print(chart)
return(chart)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project