I'm new to programming with python and I'm working on the python 4 everybody course. For that I need to build a budget app. In specific, this is the first two tasks:
Complete the Category class in budget.py. It should be able to instantiate objects based on different budget categories like food, clothing, and entertainment. When objects are created, they are passed in the name of the category. The class should have an instance variable called ledger that is a list. The class should also contain the following methods:
A deposit method that accepts an amount and description. If no description is given, it should default to an empty string. The method should append an object to the ledger list in the form of {"amount": amount, "description": description}.
A withdraw method that is similar to the deposit method, but the amount passed in should be stored in the ledger as a negative number. If there are not enough funds, nothing should be added to the ledger. This method should return True if the withdrawal took place, and False otherwise.
My attempt for now is the following:
class Category:
def __init__(self):
self.ledger = []
Total = 0
def deposit(self, amount, *description):
self.ledger.append({"amount": amount, "description": description})
return self.ledger
def withdraw(self, withdrawal):
for x in self.ledger:
if x == int():
return sum
self.ledger.append({"withdrawal": withdrawal})
return self.ledger
I think I have multiple questions:
- What is a list with {} as one Item? Is it a 5.4. "Set"?
- How can I now implement the requirement of the withdraw method "If there are not enough funds, nothing should be added to the ledger." I think I need to sum up all the integers of the list self.ledger, but idk how to grab those from it and sum it up. I tried as you can see a for loop, but I think that is incorrect syntax?
I really appreciate every help and am grateful for also some background knowledge!
Thanks in advance! Lukas