我的代码通过在菜单上输入数字(5)或数字(6)来询问用户是否要从已输入的余额中存入或提取金额。我以为我的代码可以用,但是当我存入或提取资金并在菜单上输入(1)来检查我的余额时,它得到的是原始余额,而不是提取或存款的金额。我还认为我的“每月利率”和“利率”都可以,但不能。我已经写了计算以获得月利率,但是当我想显示它们时,它似乎也不起作用。
主模块:
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
答案 0 :(得分:1)
对不起。我在打电话方面犯了一个错误。
这应该解决弹出的错误。
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)