Tell us what’s happening:
Describe your issue in detail here.
i have fixed the indentation errrors i guess.. now its fulfilling upto the category building part but is stuck from the spend chart part. please help
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, description = ''):
entry = {'amount': amount, 'description': description}
self.ledger.append(entry)
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
entry = {'amount': -amount, 'description': description}
self.ledger.append(entry)
return True
return False
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item['amount']
return balance
def transfer(self, amount, Category):
if self.withdraw(amount, f"Transfer to {Category.name}"):
Category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
return False
def __str__(self):
final = self.name.center(30, '*') + '\n'
total = 0
for item in self.ledger:
desc = item['description'][0:23]
amt = f"{item['amount']:.2f}"
final += f"{desc:<23}{amt:>7}\n"
total += item['amount']
return final + f"Total: {total}"
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant')
clothing = Category('Clothing')
food.transfer(50, clothing)
#print(food)
#print(clothing)
def create_spend_chart(categories):
total = 0
percentages = []
for category in categories:
for item in category.ledger:
amt = item['amount']
if amt < 0:
total += abs(item['amount'])
print('Percentage spent by category')
for category in categories:
spent = 0
for item in category.ledger:
if item['amount'] < 0:
spent += abs(item['amount'])
percent = (spent / total * 100) // 10 * 10
percentages.append(percent)
for num in range(100, -1, -10):
print(f"{num:>3}|", end='')
for percent in percentages:
if percent >= num:
print(" o ", end="")
else:
print(" ", end="")
print()
last_line = len(percentages)
print(' ' + '---' * last_line + '-')
category_list = []
for category in categories:
category_list.append(category.name)
longest = max(category_list, key=len)
for i in range(len(longest)):
print(' ', end='')
for category in category_list:
if len(category) > i:
print(category[i], end=" ")
else:
print(' ', end='')
print()
create_spend_chart([food, clothing])
Your mobile information:
CPH2693 - Android 15 - Android SDK 35
Challenge: Build a Budget App - Build a Budget App
Link to the challenge: