Hey, I completed this on my local vs studio and pasted it into the web browser but it didn’t want to work even though it works fine locally.
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):
balance = 0
for i in range(len(self.ledger)):
balance += self.ledger[i]['amount']
return balance
def transfer(self, amount, destination):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {destination.name}')
destination.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
def __str__(self):
total_stars = 30 - len(self.name)
if total_stars % 2 != 0:
stars_left = total_stars // 2 + total_stars % 2
stars_right = total_stars // 2 - total_stars % 2
else:
stars_left = total_stars // 2
stars_right = total_stars // 2
final_output = f'{stars_left * '*'}{self.name}{stars_right * '*'}'
def print_amount(ledger_entry):
stringified_amount = str(round((ledger_entry['amount']), 2))
nonlocal final_output
if len(stringified_amount) > 7:
final_output += f'{round((ledger_entry['amount']), 2)[:7]}'
else:
whitespace = 7 - len(stringified_amount)
final_output += f'{' ' * whitespace}{round((ledger_entry['amount']), 2)}'
for ledger_entry in self.ledger:
if len(ledger_entry['description']) > 23:
final_output += f'\n{ledger_entry['description'][:23]}'
print_amount(ledger_entry)
else:
whitespace = 23 - len(ledger_entry['description'])
final_output += f'\n{ledger_entry['description']}{' ' * whitespace}'
print_amount(ledger_entry)
final_output += f'\nTotal: {self.get_balance()}'
return final_output
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)
print(food)
clothing.withdraw(50)
def create_spend_chart(categories):
total_spent_per_category = {}
for category in categories:
for i in range(len(category.ledger)):
if category.ledger[i]['amount'] < 0:
if category.name in total_spent_per_category:
total_spent_per_category[category.name] += category.ledger[i]['amount']
else:
total_spent_per_category[category.name] = category.ledger[i]['amount']
total = sum(total_spent_per_category.values())
# Converting the total spent per category to percentages
for spent_category in total_spent_per_category:
total_spent_per_category[spent_category] = int(round((total_spent_per_category[spent_category] / total * 100), -1))
final_string = 'Percentage spent by category'
for i in range(100, -1, -10):
if len(str(i)) == 3:
final_string += f'\n{i}|'
for category in total_spent_per_category:
if i <= total_spent_per_category[category]:
final_string += ' o '
else:
whitespace = 3 - len(str(i))
final_string += f'\n{whitespace * ' '}{i}|'
for category in total_spent_per_category:
if i <= total_spent_per_category[category]:
final_string += ' o '
final_string += f'\n {(len(total_spent_per_category) * 3) * '-'}-\n'
for i in range(len(category)):
final_string += ' '
one_less = False
for category in total_spent_per_category:
if i < len(category):
if one_less:
final_string += ' '
final_string += f' {category[i]} '
else:
one_less = True
if not i == (len(category) - 1):
final_string += '\n'
return final_string
print(create_spend_chart([food, clothing]))
Here is the local output that get’s printed to the terminal:
*************Food*************
initial deposit 1000
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50
Total: 923.96
Percentage spent by category
100|
90|
80|
70|
60| o
50| o
40| o o
30| o o
20| o o
10| o o
0| o o
-------
F C
o l
o o
d t
h
i
n
g
Here’s what the browser one for freeCodeCamp outputs:
Traceback (most recent call last):
File "main.py", line 48
final_output = f'{stars_left * '*'}{self.name}{stars_right * '*'}'
^
SyntaxError: f-string: expecting '}'