Tell us what’s happening:
Not sure why step 23 and 24 are failing. I have gone thru the spacing and all looks good.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
def check_funds(self, amount):
if self.balance >= amount:
return True
else:
return False
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):
negative_amount = -amount
self.ledger.append({'amount': negative_amount, 'description': description})
self.balance -= amount
return True
else:
print('Exceeded funds')
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
else:
print('Exceeded funds')
return False
def __str__(self):
final_string = f'{self.name.center(30, "*")}\n'
for item in self.ledger:
final_string += f'{item["description"][:23]}'.ljust(23)
final_string += f'{item["amount"]:.2f}'.rjust(7)
final_string += '\n'
final_string += f'Total: {self.get_balance():.2f}'
return final_string
def create_spend_chart(categories):
chart = 'Percentage spent by category\n'
total_spent = []
category_spent = []
percentages_spent = []
y_axis = list(range(100, -10, -10))
y_axis_down = ''
x_axis = ''
category_lengths = []
for item in categories:
category_lengths.append(len(item.name))
for item in categories:
total = []
for item in item.ledger:
if item['amount'] < 0:
item = abs(item['amount'])
total_spent.append(item)
total.append(item)
total_sum = sum(total)
category_spent.append(total_sum)
total_spent = sum(total_spent)
for number in category_spent:
percent = number / total_spent * 100
percent = (percent // 10) * 10
percentages_spent.append(int(percent))
for num in y_axis:
y_axis_down += f'{str(num).rjust(3)}| '
for percent in percentages_spent:
if percent >= num:
y_axis_down += 'o '
else:
y_axis_down += ' '
y_axis_down += '\n'
if num == 0:
x_axis += f' {"---" * len(categories)}-'
letter_num = 0
while letter_num <= max(category_lengths):
x_axis += '\n'
x_axis += ' '
for item in categories:
try:
x_axis += f'{item.name[letter_num]} '
except IndexError:
x_axis += ' '
letter_num += 1
chart += y_axis_down
chart += x_axis
return chart
food = Category('Food')
clothing = Category('Clothing')
emergency = Category('Emergency')
luxury = Category('Luxury')
food.deposit(1000, 'Monthly deposit')
clothing.deposit(300, 'Monthly deposit')
emergency.deposit(500, 'Monthly deposit')
luxury.deposit(100, 'Monthly deposit')
food.withdraw(300, 'Groceries')
food.withdraw(50, 'Takeaways')
clothing.withdraw(100, 'Shirts')
emergency.withdraw(20, 'Flu')
luxury.withdraw(80, 'Vacation')
#print(food.ledger)
#print(clothing.ledger)
#print(emergency.ledger)
#print(food.balance)
#print(clothing.balance)
#food.transfer(50, clothing)
#print(food.ledger)
#print(clothing.ledger)
#print(food.balance)
#print(clothing.balance)
#print(food)
#print(clothing)
#print(emergency)
list_of_categories = [food, clothing, emergency, luxury]
print(create_spend_chart(list_of_categories))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App