Tell us what’s happening:
Hello, I think I completed the challenge but it won’t pass test 20, 23 and 24, can someone see where something is wrong?
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
#Tilføj transaction til listen
def deposit(self, amount, description=''):
self.balance += amount
self.ledger.append({'amount': amount, 'description': description})
#Træk fra kontoen
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.deposit(-amount, description)
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, another_category):
if self.check_funds(amount):
self.balance -= amount
self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_category.name}'})
another_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if amount <= self.balance:
return True
else:
return False
def __str__(self):
output = self.name.center(30,'*') + '\n'
for item in self.ledger:
amount = item['amount']
description = item['description']
output += f'{description[0:23]:23}{amount:7.2f}'
output += '\n'
output += 'Total: '
output += format(self.balance, '.2f')
return output
def create_spend_chart(categories):
categories_name = []
categories_procent = []
chartstr = 'Percentage spent by category\n'
#beregning af totalt brugt for kategori
total_withdrawal = 0
tot_spend = []
for category in categories:
categories_name.append(category.name)
cat_spend = []
for item in category.ledger:
if item['amount'] < 0:
total_withdrawal += abs(item['amount'])
cat_spend.append(abs(item['amount']))
cat_tot_spend = sum(cat_spend)
tot_spend.append(cat_tot_spend)
#Beregning af procentdele
for spend in tot_spend:
cat_procent = 0
cat_procent = spend/total_withdrawal*100
categories_procent.append(cat_procent)
#Lav grafen
for num in range(100, -10, -10):
chartstr += f"{str(num) + '|':>4}"
for procent in categories_procent:
if procent >= num:
chartstr += ' o '
else:
chartstr += ' '
chartstr += '\n'
chartstr += ' ' + (3*len(categories)+1)*'-' + '\n'
#Indsæt kategorinavne
max_length = max(len(category) for category in categories_name)
for i in range(0, (max_length + 1)):
lodret_navne = ' '
for category in categories_name:
if i < len(category):
lodret_navne += category[i] + ' '
else:
lodret_navne += ' '
lodret_navne += '\n'
chartstr += lodret_navne
output = chartstr.strip()
return output
food = Category('Food')
food.deposit(1000, 'initial deposit')
clothing = Category('Clothing')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50, clothing)
clothing.withdraw(30,'Trousers')
auto = Category('Auto')
auto.deposit(400, 'Initial deposit')
auto.withdraw(15, 'New wheels')
print(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/17.2.1 Safari/605.1.15
Challenge Information:
Build a Budget App - Build a Budget App
