Tell us what’s happening:
can you tell me what’s wrong with my code. I test the code in vscode & the output is exactly the same
Your code so far
class Category:
def __init__(self, categories):
self.categories = categories
self.ledger = []
self.amount_to_withdraw = 0
def deposit(self, amount, description=""):
if self.ledger == []:
self.ledger.append({"amount": round(float(amount), 2), "description": "Initial deposit"})
else:
self.ledger.append({"amount": round(float(amount), 2), "description": description})
def withdraw(self, amount_to_withdraw, description=""):
# print(amount_to_withdraw)
self.amount_to_withdraw = -round(float(amount_to_withdraw), 2)
# print(self.amount_to_withdraw)
if self.check_funds():
self.ledger.append({"amount": round(-float(amount_to_withdraw), 2), "description": description})
# calculate & return the amount balance
def get_balance(self):
balance = 0
for amounts in self.ledger:
balance += amounts["amount"]
return round(float(balance), 2)
# transfer the amount from object a to object b
def transfer(self, amount_to_withdraw, object_category):
self.amount_to_withdraw = round(-float(amount_to_withdraw), 2)
if self.check_funds():
self.ledger.append({"amount": self.amount_to_withdraw, "description": f"Transfer to {object_category.categories}"})
object_category.deposit(-self.amount_to_withdraw, "deposit")
# check if withdraw or transfer amount less than account balance
def check_funds(self):
amount_to_check = self.amount_to_withdraw
if -amount_to_check > self.get_balance():
return False
return True
# print the string from object
def __str__(self):
title = self.categories.center(30, "*")
ledger_after_format = [f'{x["amount"]:.2f}'.format() for x in self.ledger]
content = "\n".join([f'{str(ledger_list["description"][:23]).ljust(23)}{ledger_after_format[x].rjust(7)}' for x, ledger_list in enumerate(self.ledger)])
return f'{title}\n{content}\nTotal: {self.get_balance():.2f}'.format()
# draw a spending printed chart
def create_spend_chart(categories):
total_spending = sum(-spent["amount"] for spent in categories if spent["amount"] <= -1)
percent_spending = {cashout["description"]:(round((-cashout["amount"]/total_spending)*100)//10)*10 for cashout in categories if cashout["amount"] <= -1}
print("Percentage spent by category")
percentage = 100
while percentage > -1 :
print(f"{str(percentage).rjust(3)}{'|'.rjust(1)}{''.rjust(1)}", end='')
for x, y in percent_spending.items():
if y >= percentage:
print('o', end=('').rjust(2))
else:
print(' ', end=('').rjust(2))
print("")
percentage -= 10
# print("")
print(f"{''.rjust(4)}{('-'*((len(percent_spending)*3)+1)).ljust(9)}")
try:
max_length = max(len(word) for word in [x for x in percent_spending.keys()])
for i in range(max_length):
output_string = ("").rjust(2).join([word[i] if len(word) > i else " " for word in [x for x in percent_spending.keys()]])
print("".rjust(4), output_string)
except ValueError:
return ''
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App