My code asks the user whether he or she wants to deposit or withdraw an amount from an already entered balance by entering number (5) or number (6) on the menu. I thought my code would work, but when I deposit or withdraw money and enter (1) on the menu to check my balance it gives me the original balance and not the withdrawn or deposited amount. I also thought my Monthly Interest and Interest Rate would work, but they don't. I have written the calculations to get the monthly interest rate, but it doesn't seem to work either when I want to display them.
main module:
from Account import Account
id = float(input("Enter User ID: "))
annual_interest_rate = float(input("Enter Interest Rate: "))
balance = float(input("Enter balance: "))
def apply_actions(action, account):
if action == 0: # display ID
print(f"Your id is {id}")
elif action == 1: # display balance
print(f"Your balance is {balance}")
elif action == 2: # display annual interest rate
print(f"Your Annual Interest Rate is {annual_interest_rate}")
elif action == 3: # display monthly interest rate
print(f"Your monthly interest rate is {Account.monthly_interest_rate}")
elif action == 4: # display monthly interest
print(f"Your Monthly Interest is {Account.get_monthly_interest}")
elif action == 5: # ask for money to withdraw
to_withdraw = float(input("How much money do you want to Withdraw?"))
account.withdraw(to_withdraw)
elif action == 6: # ask for money to deposit
amount = float(input("How much money do you want to deposit?"))
account.deposit(amount)
elif action == 7: # ask to exit
exit(7)
else:
print("Bad index")
if __name__ == '__main__':
acc = Account(id, balance, annual_interest_rate)
actions = ["Display ID",
"Display Balance",
"Display Annual Interest Rate",
"Display Monthly Interest Rate",
"Display Monthly Interest",
"Withdraw Money",
"Deposit Money",
"Exit"]
while True:
choice = int(input("Choose index in " + str(list(enumerate(actions)))))
apply_actions(choice, acc)
Account.py
class Account:
def __init__(self, id, balance, annual_interest_rate):
self.id = id
self.balance = balance
self.annual_interest_rate = annual_interest_rate
def monthly_interest_rate(self):
return self.annual_interest_rate / 12
def id(self):
return self.id
def balance(self):
return self.balance
def annual_interest_rate(self):
return self.annual_interest_rate
def get_monthly_interest(self):
return self.balance * self.monthly_interest_rate
def withdraw(self, amount):
if self.balance < amount:
raise ValueError(f"Overdraft, balance less than {amount}")
self.balance -= amount
def deposit(self, amount):
self.balance +=amount
Sorry about that. I made a mistake with regards to calling.
This should fix the errors that pop up.
class Account:
def __init__(self, id, balance, annual_interest_rate):
self.id = id
self.balance = balance
self.annual_interest_rate = annual_interest_rate
def monthly_interest_rate(self):
return self.annual_interest_rate / 12
def id(self):
return self.id
def balance(self):
return self.balance
def annual_interest_rate(self):
return self.annual_interest_rate
def get_monthly_interest(self):
# NOTE: You need the () after self.balance to tell Python to use the method and not the variable, or after self.monthtly_interest_rate. Otherwise, Python takes this as a function instead of a value.
return self.balance() * self.monthly_interest_rate
def withdraw(self, amount):
if self.balance < amount:
raise ValueError(f"Overdraft, balance less than {amount}")
self.balance -= amount
def deposit(self, amount):
self.balance +=amount
id = float(input("Enter User ID: "))
annual_interest_rate = float(input("Enter Interest Rate: "))
balance = float(input("Enter balance: "))
def apply_actions(action, account):
if action == 0: # display ID
print(f"Your id is {account.id}")
elif action == 1: # display balance
print(f"Your balance is {account.balance}")
elif action == 2: # display annual interest rate
print(f"Your Annual Interest Rate is {account.annual_interest_rate}")
elif action == 3: # display monthly interest rate
print(f"Your monthly interest rate is {account.monthly_interest_rate()}")
elif action == 4: # display monthly interest
print(f"Your Monthly Interest is {account.get_monthly_interest()}")
elif action == 5: # ask for money to withdraw
to_withdraw = float(input("How much money do you want to Withdraw?"))
account.withdraw(to_withdraw)
elif action == 6: # ask for money to deposit
amount = float(input("How much money do you want to deposit?"))
account.deposit(amount)
elif action == 7: # ask to exit
exit(7)
else:
print("Bad index")
if __name__ == '__main__':
acc = Account(id, balance, annual_interest_rate)
actions = ["Display ID",
"Display Balance",
"Display Annual Interest Rate",
"Display Monthly Interest Rate",
"Display Monthly Interest",
"Withdraw Money",
"Deposit Money",
"Exit"]
while True:
choice = int(input("Choose index in " + str(list(enumerate(actions)))))
apply_actions(choice, acc)