Tell us what’s happening:
My code isn’t passing the first test: The deposit method should create a specific object in the ledger instance variable. I don’t understand why. The method seems to work, though.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.legder = []
self.total = 0.0
def get_balance(self, legder):
return self.total
def check_funds(self, amount):
total_money = 0
for trans in self.legder:
total_money += trans['amount']
if amount > total_money:
return False
else:
return True
def deposit(self, amount, description = ''):
self.legder.append({'amount': round(amount, 2), 'description': description})
self.total += amount
def withdraw(self, amount, description = ''):
if not self.check_funds(amount):
return False
else:
self.legder.append({'amount': round(amount*-1,2), 'description': description})
self.total += amount
return True
def transfer(self, amount, category):
if self.check_funds(amount) == False:
return False
else:
self.withdraw(amount, 'Transfer to ' + category.name)
category.deposit(amount, 'Transfer from ' + self.name)
def __str__(self):
chart = ''
num_stars = (30 - len(self.name)) / 2
num_stars = int(round(num_stars, 0))
asterik = '*'
asterik *= num_stars
chart += asterik + self.name.capitalize() + asterik +'\n'
total_amount = 0
for line in self.legder:
if len(line['description']) > 23:
line['description'] = line['description'][0:23]
num_spaces = 30 - len(line['description']) - len(str(line['amount']))
num_spaces *= ' '
money = round(line['amount'], 2)
chart += line['description'] + num_spaces + str(line['amount']) + '\n'
total_amount += money
chart += 'Total: ' + str(round(total_amount, 2))
return chart
def create_spend_chart(categories):
chart = 'Percentage spent per category\n'
expenses = 0
cat_total = {}
# Calculate total expenses and expenses per category
for cat in categories:
withdraw = 0
for ledger in cat.legder:
if ledger['amount'] < 0:
expenses += abs(ledger['amount'])
withdraw += abs(ledger['amount'])
cat_total[cat.name] = withdraw
# Calculate percentages per category
for i in cat_total:
per = (cat_total[i] / expenses) * 100
per = round(per, 2)
cat_total[i] = per
# Print the y-axis and the vertical bars
for num in range(100, -10, -10):
if num == 0:
chart += ' ' + ' ' + str(num) + '|'
elif num != 100:
chart += ' ' + str(num) + '|'
else:
chart += str(num) + '|'
for per in cat_total.values():
if num <= per:
chart += ' o '
else:
chart += ' '
chart += '\n'
x = ' '
for cat in categories:
for i in range(len(cat.name)):
x += ' ' + cat.name[i] + ' '
chart += ' ' +'-' * (len(cat_total) * 3 + 1)
return chart
food = Category('Food')
clothing = Category('Clothing')
sport = Category('Sport')
hobby = Category('Hobby')
food.deposit(30.40, 'work')
food.withdraw(12.90, 'snacks')
clothing.deposit(50, 'pocket money')
clothing.withdraw(29.95, 't-shirt')
sport.deposit(300, 'sponsor')
sport.withdraw(230, 'tennis racket')
hobby.deposit(150, 'pocket money')
hobby.withdraw(70, 'art supplies')
food.transfer(10, clothing)
clothing.transfer(13.75, food)
sport.transfer(17.63, hobby)
hobby.transfer(13.90, sport)
#print(str(food))
#print(str(clothing))
#print(str(sport))
#print(str(hobby))
print(create_spend_chart([food, clothing, sport, hobby]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App