Tell us what’s happening:
I have tried for days and cannot pass this test: “The transfer method should increase the balance of the category object passed as its argument.” I don’t know what I’m doing wrong. Also, I don’t know if my calculations for the percent chart is correct. I am supposed to be getting one ‘o’ for the ‘Business’ column but I’m getting two. I feel like I am correct but the module is telling me otherwise. Please help!
Your code so far
cats = []
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.total = 0
self.withdrawals = 0
cats.append(self)
def __str__(self):
result = self.category.center(30, '*') + '\n'
for item in self.ledger:
descr_length = len(item['description'])
if descr_length > 23:
item['description'] = item['description'][:23]
descr_length = len(item['description'])
if (item['amount'] * 10) % 1 == 0 or item['amount'] == 0: # one decimal place, integers, or equal to zero
item['amount'] = str(item['amount']) + '0'
else:
item['amount'] = item['amount'] # two decimal places
if len(str(item['amount'])) > 7:
item['amount'] = item['amount'][0:7]
result += f"{item['description']}{item['amount']:>{30 - descr_length}}"
result += '\n'
self.total = round(self.total * 100) / 100
result += f'Total: {self.get_balance()}'
return result
def deposit(self, amount, description = ''):
amount = (round(amount * 100)) / 100 # for 3 or more decimal places
self.ledger.append({'amount': amount, 'description': description})
self.total += amount
def withdraw(self, amount, description = ''):
amount = (round(amount * 100)) / 100
if self.check_funds(amount):
self.ledger.append({'amount': amount/-1, 'description': description})
self.total -= amount
self.withdrawals += amount
return True
return False
def get_balance(self):
if (self.total * 10) % 1 == 0 or self.total == 0:
return str(self.total) + "0"
else:
return self.total
def transfer(self, amount, to):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {to.category}')
to.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def check_funds(self, amount):
if amount > self.total:
return False
else:
return True
def create_spend_chart(list_of_cats):
if len(list_of_cats) > 4: return 'Please input max 4 categories'
result = 'Percentage spent by category\n'
x_axis = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
y_axis = []
total_dashes = len(list_of_cats) + 7
withdrawal_total = 0
longest_cat = ''
for cat in list_of_cats:
withdrawal_total += cat.withdrawals
if len(cat.category) > len(longest_cat):
longest_cat = cat.category
for cat in list_of_cats:
if withdrawal_total > 0:
cat_percent = (cat.withdrawals / withdrawal_total) * 100
cat_percent_tens = (round(cat_percent/10)) * 10
#if cat_percent_tens <= 10: # these lines are necessary in order to pass the last test
#cat_percent_tens = 0
y_axis.append([cat.category, cat_percent_tens])
print("Withdrawal total:" , withdrawal_total)
print(y_axis)
for num in x_axis:
result += f'{num:>3}| '
for percent in y_axis:
if num > percent[1]:
result += f'{" ":>3}'
if num <= percent[1]:
result += 'o '
result += '\n'
result += ' ' # spaces before dashed line
for dash in range(total_dashes):
result += '-'
result += '\n'
for index in range(len(longest_cat)):
result += ' '
for cat in y_axis:
try:
result += f'{cat[0][index]} '
except:
result += f'{" "} '
result += '\n'
return f'{result.rstrip()} '
food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)
print(create_spend_chart([business, food, entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project