Tell us what’s happening:
Hello
I have completed the code. For all I know it is working as intended but I am not passing the last test. The instructions are to account for withdrawals but don’t mention transfers, I am taking those into account as well.
I have seen some post of people testing their code with replit but I don’t know how to do that.
Thanks
Your code so far
class Category:
def __init__(self, name):
self.cat = name
self.ledger = []
self.balance = 0.0
def __str__(self):
output = f'{self.cat:*^30}\n'
for item in self.ledger:
output += f'{item["description"][:23]:<23}{float(item["amount"]):>7.2f}\n'
output += 'Total: ' + str(self.balance)
return output
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):
self.balance -= amount
self.ledger.append({'amount':-amount, 'description':description}
)
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, destination):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {destination.cat}')
destination.deposit(amount, f'Transfer from {self.cat}')
return True
else:
return False
def check_funds(self, amount):
if self.balance >= amount :
return True
else:
return False
def create_spend_chart(categories):
n = len(categories)
total_expenses = 0
expenses = []
def spent(category):
spent = 0
for item in category.ledger:
if float(item["amount"]) < 0:
spent -= float(item["amount"])
return spent
# Spent por categoría
for category in categories:
expenses.append(spent(category))
total_expenses += spent(category)
expenses_tuple = list(zip([category.cat for category in categories], [int(100*spent(category)/total_expenses // 10) for category in categories]))
# Create string
percentages = f'Percentage spent by category\n'
for i in range(10):
percentages += f'{str(100-10*i):>3}|'
line = ''
for category in expenses_tuple:
if category[1] >= 10 - i:
line += ' o '
else:
line += ' '
percentages += line + ' \n'
percentages += 4*' ' + (3*n+1)*'-' + '\n'
l = max([len(item[0]) for item in expenses_tuple])
for i in range(l):
percentages += 5*' '
for category in expenses_tuple:
if i < len(category[0]):
percentages += category[0][i] + 2*' '
else:
percentages += 3*' '
percentages += '\n' if i + 1 < l else ''
return(percentages)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App