Tell us what’s happening:
In my create_spend_chart function, it won’t return the chart variable to the console, but whenever I print(chart), it shows up on the console just fine. I’m not sure what I’m missing.
### Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
self.withdrawals = 0
#Turn object into string
def __str__(self):
self.ledger_str = ((((30 - len(self.name)) // 2) + (30 - len(self.name)) % 2) * '*') + self.name + (((30 - len(self.name)) // 2) * '*') + '\n'
#Break up descriptions and amounts
for item in self.ledger:
description, amount = item['description'], float(item['amount'])
amount = '%.2f' % amount
if len(description) > 23:
description = description[0:23]
if len(amount) > 7:
amount = amount[:6]
self.ledger_str += description + (30 - (len(description) + len(amount))) * ' ' + amount + '\n'
#Create Total Line
self.balance = '%.2f' % self.balance
total = f'Total: {self.balance}'
self.ledger_str += total
return self.ledger_str
#Deposit Method
def deposit(self, amount, description = ""):
self.description = description
self.amount = amount
if amount < 10000:
self.ledger.append({'amount': amount, 'description': description})
self.balance += self.amount
else:
return False
#Withdraw Method
def withdraw(self, amount, description = ""):
self.amount = amount
self.description = description
if self.balance - self.amount < 0:
return False
elif self.amount >= 1000:
return False
else:
self.ledger.append({'amount': 0 - self.amount, 'description': description})
self.balance -= self.amount
self.withdrawals += self.amount
return True
#Get Balance Method
def get_balance(self):
return self.balance
#Transfer Method
def transfer(self, amount, other_cat):
self.amount = amount
self.other_cat = other_cat
if self.balance - self.amount > 0:
self.withdraw(amount, f'Transfer to {other_cat.name}')
other_cat.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
#Check Funds Method
def check_funds(self, amount):
if self.balance - amount < 0:
return False
else:
return True
#Test Category
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(500, 'Groceries')
food.withdraw(15.49, 'restaurant and more food items')
clothing = Category('Clothing')
clothing.deposit(400, 'initial deposit')
clothing.withdraw(200, 'shoes')
auto = Category('Auto')
auto.deposit(200, 'initial deposit')
auto.withdraw(100, 'repairs')
food.transfer(50, clothing)
#print(food)
#print(clothing)
#print(auto)
def create_spend_chart(categories):
#Print Title Line on top
#chart = ''
chart = 'Percentage spent by category\n'
#Calculate Percentages
total = 0
for index in categories:
total += index.withdrawals
percent = [int((((cat.withdrawals / total) * 100) // 10) * 10) for cat in categories]
#print(percent)
#print(total)
n = 100
while n >= 0:
if n == 100:
chart += f'{n}| '
n -= 10
for i in percent:
if i == n:
chart += f'o '
else:
chart += ' '
chart += '\n'
elif n == 0:
chart += f' {n}| '
n -= 10
for i in percent:
chart += 'o '
chart += '\n'
else:
chart += f' {n}| '
for i in percent:
if i >= n:
chart += 'o '
else:
chart += ' '
n -= 10
chart += '\n'
#Make bottom line
bottom_line = ' _'
bottom_line += (len(categories) * '___')
chart += bottom_line + '\n'
#Make categories vertical
bar_title = ' '
max_len = 0
for cat in categories:
if len(cat.name) > max_len:
max_len = len(cat.name)
#print(max_len)
for cat in categories:
while len(cat.name) < max_len:
cat.name += ' '
index = 0
letter_num = 0
for categories[index] in categories:
while letter_num < max_len:
if index < len(categories) - 1:
bar_title += categories[index].name[letter_num] + ' '
index += 1
else:
if letter_num < max_len - 1:
bar_title += categories[index].name[letter_num] + ' ' + '\n' + ' '
letter_num += 1
index = 0
else:
bar_title += categories[index].name[letter_num] + ' '
letter_num += 1
chart += bar_title
return chart
create_spend_chart([food, clothing, auto])
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Mobile Safari/537.36 EdgA/132.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project