Hello everybody!
I have a serious problem with this exercise, I succeeded all the tests but when I test with my own tests it does not work.
The problem is at the level of the last function, in the test everything is ok but when I try it with the code for the exercise all the lines are offset from a space except line 0.
I would be very grateful to you to enlighten me on this problem which seems to me to be like magic …
thanks in advance
link to the code “boilerplate-budget-app - Replit”
My code :
import math
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
# line 1
name = str(self.name)
len_name = len(name)
stars = '*' * int((30 - len_name) / 2)
line_1 = stars + name + stars + '\n'
# line 2
line_2 = ''
for operation in self.ledger:
description_str = operation['description'][0:23]
amount_str = "{:.2f}".format(operation['amount'])[:7]
len_description = len(description_str)
len_amount = len(amount_str)
space = 30 - len_description - len_amount
line_2 += description_str + (' ' * space) + amount_str + '\n'
# line 3
line_3 = 'Total: ' + str(self.get_balance())
# result
result = line_1 + line_2 + line_3
return str(result)
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=''):
fund_enough = self.check_funds(amount)
if not fund_enough:
return False
else:
self.ledger.append(
{'amount': - amount, 'description': description})
return True
def get_balance(self):
total = 0
for operation in self.ledger:
# print(operation)
total += operation['amount']
return total
def transfer(self, amount, category):
fund_enough = self.check_funds(amount)
if not fund_enough:
return False
else:
self.withdraw(amount, 'Transfer to ' + str(category.name))
category.deposit(amount, 'Transfer from ' + str(self.name))
return True
def check_funds(self, amount):
total = self.get_balance()
if total - amount < 0:
return False
else:
return True
def create_spend_chart(categories):
line_4 = ''
list_name = []
len_category = 0
for category in categories:
name = str(category.name)
list_name.append(name)
if len(name) > len_category:
len_category = len(name)
# calcul percentage
arr_only_depenses = []
for category in categories:
arr_only_depenses.append(get_only_depenses(category))
# print('arr_only_depenses: ', arr_only_depenses)
total_only_depenses = 0
for depense in arr_only_depenses:
total_only_depenses += abs(depense)
# print('total_only_depenses: ', total_only_depenses)
arr_percentage = []
for category in categories:
depense_positif = abs(get_only_depenses(category))
percentage = depense_positif / total_only_depenses * 100
percentage_round = math.floor(percentage / 10) * 10
arr_percentage.append(percentage_round)
print('arr_percentage: ', arr_percentage)
# line1
line_1 = 'Percentage spent by category' + '\n'
# line_2
line_2 = ''
i = 110
max_len = (len(list_name) * 3) + 1
print('max_len: ', max_len)
while i > 0:
i -= 10
space_before_calcul = abs(int(len(str(i))) - 3)
space_before = '*' * space_before_calcul
space_after = '*' * max_len
line_actuel = space_before + str(i) + '|' + space_after + '\n'
print('line_actuel: ', line_actuel)
for j, percentage in enumerate(arr_percentage):
if i <= percentage:
arr_line_actuel = line_actuel.split('*')
print('arr_line_actuel: ', arr_line_actuel)
calcul = 3 + (j*2)
print('j: ', j)
print('calcul: ', calcul, i)
arr_line_actuel[calcul] = 'o'
arr_line_actuel.pop(-3)
# print('arr_line_actuel: ', arr_line_actuel)
line_actuel = '*'.join(arr_line_actuel)
# print('line_actuel: ', line_actuel)
line_actuel = line_actuel.replace('*', ' ')
line_2 += line_actuel
# line_3
len_categories = len(categories)
# print('len_categories: ', len_categories)
dash = '-' * ((len_categories * 3) + 1)
# print('dash: ', dash)
line_3 = ' ' + dash
i = 0
for character in range(len_category):
line_5 = ''
for name in list_name:
space = ' ' * list_name.index(name)
# print('space: ', space)
if len(name) > i:
line_5 += ' ' + name[i] + ' '
else:
line_5 += ' '
line_4 += '\n' + ' ' + line_5 + ' '
i += 1
# result
result = line_1 + line_2 + line_3 + line_4
return str(result)
def get_only_depenses(category):
only_depenses = 0
for operation in category.ledger:
# print(operation)
if operation['amount'] < 0:
only_depenses += operation['amount']
return only_depenses
Challenge: Scientific Computing with Python Projects - Budget App
Link to the challenge: