gpt4 book ai didi

python - 账户存款和取款

转载 作者:行者123 更新时间:2023-12-01 06:59:51 25 4
gpt4 key购买 nike

我的代码询问用户是否想通过在菜单上输入数字 (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)

帐户.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

最佳答案

对此感到抱歉。我在打电话时犯了一个错误。

这应该可以修复弹出的错误。

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)

关于python - 账户存款和取款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com