Tell us what’s happening:
Just wondering if my format for this is right.
How do i shift items to the left or right also.
Thanks
Your code so far
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.budget = 0
def deposit(self, amount, description = ''):
self.budget += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.budget -= amount
return True
else:
return False
def get_balance(self):
return self.budget
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.budget -= amount
self.ledger.append({'amount': -amount, 'description': f'Transfer to {other_category.category}'})
other_category.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
def __str__(self):
category_print = ''
category_print += self.category.center(30, '*')
category_print += '\n'
for item in self.ledger:
description = item['description']
amount = str(item['amount'])
category_print += f'{description[0:23]:<23}'
category_print += f'{amount[0:7]:>7}'
category_print += '\n'
category_print += 'Total: '
category_print += f'{self.get_balance()}'
category_print += '\n'
return category_print
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
print(clothing)
def create_spend_chart(categories):
pass
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App
ILM
2
here you are shifting things on one side or the other, do it again for the total
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.budget = 0
def deposit(self, amount, description = ''):
self.budget += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.budget -= amount
return True
else:
return False
def get_balance(self):
return self.budget
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.budget -= amount
self.ledger.append({'amount': -amount, 'description': f'Transfer to {other_category.category}'})
other_category.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
def __str__(self):
category_print = ''
category_print += self.category.center(30, '*')
category_print += '\n'
for item in self.ledger:
description = item['description']
amount = str(item['amount'])
category_print += f'{description[0:23]:<23}'
category_print += f'{amount[0:7]:>7}'
category_print += '\n'
category_print += f'Total: '
category_print += f'{self.get_balance():<23}'
return category_print
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
print(clothing)
def create_spend_chart(categories):
pass
I did that and it still is not formatted correctly
dhess
4
Are you sure the balance should be the same width as the description?
What should it be I dont see it
dhess
6
Doesn’t it just need to be left-aligned? And shouldn’t your amounts be formatted with two decimal places?
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.budget = 0
def deposit(self, amount, description = ''):
self.budget += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
self.budget -= amount
return True
else:
return False
def get_balance(self):
return self.budget
def transfer(self, amount, other_category):
if self.check_funds(amount):
self.budget -= amount
self.ledger.append({'amount': -amount, 'description': f'Transfer to {other_category.category}'})
other_category.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def check_funds(self, amount):
if self.get_balance() >= amount:
return True
else:
return False
def __str__(self):
category_print = ''
category_print += self.category.center(30, '*')
category_print += '\n'
for item in self.ledger:
description = item['description']
amount = str(item['amount'])
if '.' not in amount:
amount = (f'{amount}.00')
category_print += f'{description[0:23]:<23}'
category_print += f'{amount:>7}'
category_print += '\n'
category_print += f'Total: '
category_print += f'{self.get_balance():<10}'
return category_print
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
print(clothing)
def create_spend_chart(categories):
pass
I added the decimals to the amounts.
Now I am still wondering the amount I need to align the total to, thanks.
ILM
8
look at the example, does your code make an output similar to that example? if there is some small difference, change the number until it’s identical