Tell us what’s happening:
Hi.
Could someone help me with this one ?
in the console it says i have 1 more space than it should be but i have no clue how to remove this space
Your code so far
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.balance = 0
self.spent = 0
def __str__(self):
string = ""
total_length = 30
stars_needed = total_length - len(self.category)
left_stars = stars_needed // 2
right_stars = stars_needed - left_stars
string += '*' * left_stars + self.category + '*' * right_stars + '\n'
for dicts in self.ledger:
if dicts.get('amount'):
string += (f'{dicts.get("description") [:23]:<23}{dicts.get("amount"):>7.2f}\n')
string += f'Total: {self.balance}'
return string
def deposit(self,amount, description = ''):
try:
self.ledger.append({'amount' : float(round(amount, 2)), 'description' : description})
self.balance += amount
except ValueError:
print("Input is not an int type")
pass
def withdraw(self,amount, description = ''):
try:
if self.check_funds(amount):
self.ledger.append({'amount' : float(round(-amount, 2)), 'description' : description})
self.balance -= amount
self.spent += amount
return True
else:
return False
except ValueError:
print("Input is not an int type")
return False
def get_balance(self):
return self.balance
def transfer(self,amount,category):
try:
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.category}")
category.deposit(amount, f"Transfer from {self.category}")
return True
else:
return False
except ValueError:
print("Input is not an int or float type")
def check_funds(self, amount):
if self.balance >= amount:
return True
else:
return False
def create_spend_chart(categories):
total_spent = sum(category.spent for category in categories)
percentages = [int(category.spent / total_spent * 100 // 10) * 10 for category in categories]
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
row = f"{i:>3}|"
for percent in percentages:
if percent >= i:
row += " o "
else:
row += " "
chart += row + " \n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_len = max(len(category.category) for category in categories)
for i in range(max_len):
row = " "
for category in categories:
if i < len(category.category):
row += category.category[i] + " "
else:
row += " "
chart += row.rstrip() + " \n"
return chart
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(90.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(1000, 'deposit')
clothing.withdraw(23.12, 'tshirt')
clothing.withdraw(32.39, 'hudie')
food.transfer(50, clothing)
auto = Category('Auto')
auto.deposit(1000, 'deposit')
auto.withdraw(1.23, 'liquid')
auto.withdraw(11.23, 'liquid')
auto.withdraw(12.23, 'liquid')
auto.withdraw(12.23, 'liquid')
print(food)
print(clothing)
print(create_spend_chart([food, clothing, auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 OPR/113.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project