Tell us what’s happening:
My “Percentage spent by category” chart looks correct to me, including the spacing, and it’s created dynamically by the number of categories passed so should be fine in testing. I read through the console logs, and the errors all point to invalid f-strings, but since I get what looks like correct output, I don’t think my f-strings are invalid. I read through many of the posted problems other learners had, and don’t see similar console output to mine. Any guidance will be appreciated.
Your code so far
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
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def get_balance(self):
balance = 0
for tx in self.ledger:
balance += tx['amount']
return balance
def transfer(self, amount, other):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': f'Transfer to {other.name}'})
other.ledger.append({'amount': amount, 'description': f'Transfer from {self.name}'})
return True
return False
def __str__(self):
print_string = ''
available_width = 30 - len(self.name)
padding_left = available_width//2
padding_right = 30 - len(self.name) - padding_left
print_string += f"{('*' * padding_left)}{self.name}{('*' * padding_right)}\n"
for tx in self.ledger:
description = tx['description'][:23]
amount = f"{tx['amount']:.2f}".rjust(7)
print_string += f"{description.ljust(23)}{amount}\n"
print_string +=f"Total: {self.get_balance()}"
return print_string
def create_spend_chart(categories):
print('Percentage spent by category')
y_labels = [n for n in range(100, -1, -10)]
withdrawals = []
denominator = 0
for category in categories:
wd = 0
for tx in category.ledger:
if (tx['amount'] < 0):
wd -= tx['amount']
withdrawals.append((category.name, wd))
denominator += wd
percentages = []
for item in withdrawals:
percentages.append((round(item[1]/denominator, 1))*100)
for y in y_labels:
line = f"{y}| ".rjust(5)
for percent in percentages:
if percent >= y:
line +="o "
else:
line +=" "
print(line)
print(f" --{'---' * (len(categories) - 1)}--")
max_length = max(len(cat.name) for cat in categories)
for i in range(max_length):
line = " "
for category in categories:
if i < len(category.name):
line += f"{category.name[i]} "
else:
line += " "
print(line)
food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(700)
food.withdraw(645)
clothing.deposit(300)
clothing.withdraw(255)
auto.deposit(140)
auto.withdraw(100)
create_spend_chart([food, clothing, auto])
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15 Ddg/26.1
Challenge Information:
Build a Budget App - Build a Budget App