Tell us what’s happening:
Hello!
“Your code raised an error before any tests could run. Please fix it and try again.”
Can someone help me with this? I can’t see anything wrong.
Your code so far
class Category:
total_expenses = []
def __init__(self, name):
self.name = name
self.ledger = []
###################DEPOSIT#####
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
####################WITHDRAW###
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
return True
else:
return False
###############GET_BALANCE#####
def get_balance(self):
total = 0
for amount in self.ledger:
total += float(amount['amount'])
return total
#############TRANSFER##########
def transfer(self, amount, another_category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {another_category.name}')
another_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
##################CHECK_FUNDS##
def check_funds(self, amount):
#se o dinheiro para retirar ou para transferir for maior que o total disponível:
if float(amount) > self.get_balance():
return False
else:
return True
##############PERCENTAGE#######
def get_expenses(self):
expenses = 0
for x in self.ledger:
val = x["amount"]
if val < 0:
expenses += -val
expenses = round(expenses, 2)
Category.total_expenses.append(expenses)
return Category.total_expenses
######################STRING###
def __str__(self):
amount = ''
description = ''
output = f'{self.name.center(30, "*")}\n'
for q in self.ledger:
amount = "{:.2f}".format(q["amount"])[:7]
description = q["description"][:23]
#if description == "deposit":
#description = "initial deposit"
output += f'{description.ljust(23)[:23]}{amount.rjust(7)[:7]}\n'
return output + f'Total: {"{:.2f}".format(self.get_balance())}'
####################MAGIC######
def __repr__(self):
return f'\n\nTotal Expenses: {self.get_expenses()}\n'
###############################
food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
food.transfer(50, clothing)
clothing.withdraw(23.44, "pants")
clothing.withdraw(18.22, "blouse")
entertainment = Category("Entertainment")
entertainment.deposit(500, "deposit for fun")
entertainment.withdraw(12.05, "cinema")
entertainment.withdraw(24.55, "meeting")
entertainment.transfer(120, clothing)
print(food)
print(clothing)
print(entertainment)
###############################
def create_spend_chart(categories):
if len(categories) > 4:
return "Impossible! Up to four categories!"
###############################
###############################
percentages = []
food_percent = 0
clothing_percent = 0
business_percent = 0
enter_percent = 0
expenses_val = Category(categories).get_expenses()
# [76.04, 41.66, 156.6, 0]
expenses_cat = []
total_expenses = sum(expenses_val)
#print(f'\n\nTotal expenses: ${"{:.2f}".format(total_expenses)}')
###############################
for category in categories:
expenses_cat.append(category)
#print(expenses_cat)
#print(expenses_val)
for category in expenses_cat:
if category == "Food":
expenses_val[0] = "{:.2f}".format(expenses_val[0])
#print(f'Food expenses: ${expenses_val[0]}')
food_percent = round(((float(expenses_val[0])*100)/float(total_expenses))/10)*10
percentages.append(food_percent)
elif category == "Clothing":
expenses_val[1] = "{:.2f}".format(expenses_val[1])
#print(f'Clothing expenses: ${expenses_val[1]}')
clothing_percent = round(((float(expenses_val[1])*100)/float(total_expenses))/10)*10
percentages.append(clothing_percent)
elif category == "Entertainment":
expenses_val[2] = "{:.2f}".format(expenses_val[2])
#print(f'Entertainment expenses: ${expenses_val[2]}')
enter_percent = round(((float(expenses_val[2])*100)/float(total_expenses))/10)*10
percentages.append(enter_percent)
elif category == "Business":
expenses_val[3] = "{:.2f}".format(expenses_val[3])
#print(f'Business expenses: ${expenses_val[3]}')
business_percent = round(((float(expenses_val[3])*100)/float(total_expenses))/10)*10
percentages.append(business_percent)
else:
print("Category not included!!")
# calculate percentage:
# 274.30 = 100%
# 76.04 = ?
# (274.30)x = 76.04 * 100
# (274.30)x = 7604
# x = 7604 ÷ 274.30
# x = 27.72 ~ 28 %
# The answer is: 28%
print(f'Food expenses: {food_percent}%\nClothing expenses: {clothing_percent}%\nEntertainment expenses: {enter_percent}%\nBusiness expenses: {business_percent}%')
###############################
#chart = "Percentage spent by category\n"
#####CALLING THE FUNCTION######
create_spend_chart(['Food', 'Entertainment', 'Business', 'Clothing'])
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project