Tell us what’s happening:
I’m unable to pass these 3 test-cases. 20, 23 and 24 and these test cases show spacing problems
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
#deposit
def deposit(self, amount, description=''):
self.ledger.append({
'amount': amount,
'description': description
})
# get balance
def get_balance(self):
balance = 0
for transactions in self.ledger:
balance += transactions['amount']
return balance
#check funds / amount in bank
def check_funds(self, amount):
if amount <= self.get_balance():
return True
return False
#withdraw amount and store it as negative
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger.append({
'amount': -amount,
'description': description
})
return True
return False
#tranfer amount from 1 category to another
def transfer(self, amount, category):
if not self.check_funds(amount):
return False
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
#list of spendings
def __str__(self):
output = self.name.center(30, '*')
for transactions in self.ledger:
output += f"\n{transactions['description'][:23]:<23}{transactions['amount']:>7.2f}"
output += f"\nTotal: {self.get_balance():.2f}"
return output
def create_spend_chart(categories):
spent = []
for category in categories:
total_spent = 0
for transaction in category.ledger:
if transaction['amount'] < 0:
total_spent += abs(transaction['amount'])
spent.append(total_spent)
total = sum(spent)
percentages = []
for amount in spent:
percentage = int((amount / total) * 100)
percentage = (percentage // 10) * 10
percentages.append(percentage)
chart = "Percentage spent by category\n"
# Bar - Chart
#Y-axis
for level in range(100,-1,-10):
line = f"{level:>3}|"
# Bars
for percentage in percentages:
if percentage >= level:
line += ' o '
else:
line += ' '
line += ' ' #2 spaces after final bar
chart += line + '\n'
#X-axis
chart += ' ' + '-' * (len(categories)*3+1) + '\n'
#Printing Categories names vertically on X-axis
#Finding out the longest name
max_length = max(len(category.name) for category in categories)
for i in range(max_length):
line = ' '
for category in categories:
if i < len(category.name):
line += category.name[i] + ' '
else:
line += ' '
line += ' ' #2 spaces after final category's name
chart += line
return chart.rstrip('\n')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:153.0) Gecko/20100101 Firefox/153.0
Challenge Information:
Build a Budget App - Build a Budget App