Tell us what’s happening:
I have been working on this for a week but I am still stuck on solving steps 19, 20, 23 and 24.
To me, I rounded the bars columns to the nearest ten using round() and -1, but for some reasons it does not pass. I tried using F12, but its the first time I use it and have no clue how it works
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
# the string value of the object
def __str__(self):
# title part
full_display = ''
empt = ' '
dot = '*'
dot_1 = round((30 - len(self.name)) / 2)
dot_2 = 30 - len(self.name) - dot_1
title = f'{dot_1 * dot}{self.name}{dot_2 * dot}\n'
full_display += title
# list part
# first, we reach each dict
for transaction in self.ledger:
descrip = transaction['description']
# I don't believe it was really needed, but it works
initial_value = (transaction['amount'])
value = f"{initial_value: .2f}"
# as long as it works...
if len(descrip) > 23:
descrip = descrip[:23]
space = 30 - len(descrip) -len(str(value))
full_display += (f'{descrip}{space*empt}{value}\n')
else:
space = 30 - len(descrip) -len(str(value))
full_display += (f'{descrip}{space*empt}{value}\n')
# out of for loop
full_display += f'Total: {self.balance}'
# It will return the full list of transactions, transfers and current balance
return full_display
def deposit(self, amount, description = ''):
self.balance += amount
self.ledger.append({
'amount': amount,
'description': description
})
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.balance -= amount
self.ledger.append({
'amount': -amount,
'description': description
})
return True
else:
return False
def get_balance(self):
return self.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.balance:
return True
else:
return False
# end of category class
def create_spend_chart(categories):
full_chart = ''
chart_title = 'Percentage spent by category\n'
space =' '
full_chart += chart_title
withdraw_per_category = []
for category in categories:
withdraw_category = 0 # it reset for each category
for transaction in category.ledger:
initial_value = (transaction['amount'])
if initial_value < 0:
withdraw_category += initial_value
withdraw_per_category.append( withdraw_category)
# GOOD: print(f'in {category.name}, you spent: {withdraw_category}')
total_withdrew = sum(withdraw_per_category)
# GOOD: print(f'in total, you spent: {total_withdrew}')
data_percent_spent = []
for withdraw_category in withdraw_per_category:
spent_on_category = int(round((withdraw_category*100) / total_withdrew, -1))
data_percent_spent.append(spent_on_category)
# GOOD: print('Percent spent per category',data_percent_spent)
# withdraw percent:
percent_point = 'o'
# percentage part:
for percent in range(100, -1, -10):
points = ''
for percent_category in data_percent_spent:
if percent_category >= percent:
points += f'{percent_point}{2*space}'
else:
points += 3*space
line_data = f"{percent:>3}| {points}{2*space}"
#line_lenght = 5 + 3*len(data_percent_spent)
#full_line_data = line_data + space*(line_lenght - len(line_data))
#print(len(full_line_data))
full_chart += line_data
full_chart += '\n'
# second part
# seperating line
bar = '---'
seperating_line = f'{4*space}{bar*(len(data_percent_spent))}-\n'
full_chart += seperating_line
# The categories displayed horizentally:
# finding the longest name:
longest_name = 0
for category in categories:
if len(category.name) > longest_name:
longest_name = len(category.name)
else:
continue
for i in range(longest_name):
# starting indention
text_line = 5*space
for category in categories:
if i < len(category.name):
text_line += f'{category.name[i]}{2*space}'
else:
text_line += f'{3*space}'
text_line += '\n'
full_chart += text_line
full_chart = full_chart.strip()
return full_chart
# testing area:
# Food Category
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(620, 'groceries')
#Clothing category
clothing = Category('Clothing')
food.transfer(1, clothing)
clothing.deposit(1000, 'initial deposit')
clothing.withdraw(204, 'restaurant and more food for dessert')
#Auto category
auto = Category('Auto')
clothing.transfer(1, auto)
auto.deposit(100000, 'initial deposit')
auto.withdraw(124, 'groceries')
auto.transfer(1, food)
stuff_list = [food, clothing, auto]
print(create_spend_chart(stuff_list))
Here is my output:
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:151.0) Gecko/20100101 Firefox/151.0
Challenge Information:
Build a Budget App - Build a Budget App
https://www.freecodecamp.org/learn/python-v9/lab-budget-app/build-a-budget-app
