Tell us what’s happening:
Hello. I’m building the create_spend_chart function. I’m can’t get following test passing:
"The height of each bar on the
create_spend_chartchart should be rounded down to the nearest 10.
I’m already tried a lot. (for past like 2 days). my originalifstatement wasif percentage >= fixed_numberbut because I couldn’t get the test passing I changed it toif math.floor(percentage/10)*10 >= fixed_numberbut still can’t get it pass. thanks for your help.
Your code so far
import math
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, description=""):
self.ledger.append({
'amount': amount,
'description': description
})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.ledger.append({
'amount': -amount,
'description': description
})
return True
else:
return False
def get_balance(self):
balance = 0;
for item in self.ledger:
balance += item['amount']
return balance
def transfer(self, amount, destination):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {destination.name}")
destination.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def __str__(self):
string = ""
max_length = 30
name_length = len(self.name)
star_count = (max_length - name_length) // 2
string += '*' * star_count + self.name + '*' * star_count + '\n'
for entry in self.ledger:
fixed_desc = entry['description'][:23]
fixed_amount = "%.2f" % entry['amount']
remained_space = 30 - len(fixed_desc) - len(str(fixed_amount))
string += fixed_desc + ' ' * remained_space + fixed_amount + '\n'
string += f"Total: {self.get_balance()}"
return string
def get_spent_money(self):
sum = 0
if self.ledger != []:
for item in self.ledger:
if item['amount'] < 0:
sum -= item['amount']
return sum
def get_deposited_money(self):
sum = 0
if self.ledger != []:
for item in self.ledger:
if item['amount'] > 0:
sum += item['amount']
return sum
def get_spent_percentage(self):
return (self.get_spent_money() * 100) // self.get_deposited_money()
def create_spend_chart(categories):
output = ""
output += "Percentage spent by category\n"
percentages = [category.get_spent_percentage() for category in categories]
for number in reversed(range(11)):
fixed_number = number * 10
if fixed_number == 100:
line = "100|"
elif fixed_number == 0:
line = " 0|"
else:
line = f" {fixed_number}|"
for percentage in percentages:
if percentage >= fixed_number:
line += " o "
else:
line += " "
expected_length = 5 + 3 * len(categories)
output += line + ' ' * (expected_length - len(line)) + "\n"
line = " "
for percentage in percentages:
line += "---"
output += line + "-\n"
name_max_length = max([len(category.name) for category in categories])
for character_number in range(name_max_length):
line = " "
for category in categories:
if len(category.name) >= (character_number + 1):
line += ' ' + category.name[character_number] + ' '
else:
line += ' '
output += line + ' \n'
output = output.removesuffix('\n')
return output
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Challenge Information:
Build a Budget App - Build a Budget App