- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码询问用户是否想通过在菜单上输入数字 (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/
我有一个银行余额文本字段,我想根据存款/取款不断更新。这是我的代码: Bank Balance: Deposit Withdraw var balance = document.getEl
我对让 BankAccount 类实现安全提款/存款功能有疑问。到目前为止,当您向bankAccount 类提款或存款时,它会打印日志。无论如何,我的问题是如何实现安全性,例如您提取的金额不得超过您银
假设我有一个合约函数,它期望通过某个交易发送一定数量的near,该函数称为create_order , create_order需要几个论点。 我在前端以 myContract 的名称设置了我的契约(
我有一个java作业。我的任务是建立一个可以取款、存款和查询余额的银行。我的问题是,我无法在存款和取款后更新我的余额......我已经尝试了我能做的一切,但仍然无法理解逻辑。有人可以帮助添加到我的程序
我用 WooCommerce Deposits插件并希望隐藏除“本地取货”运输方式之外的所有其他运输方式,如果客户选择支付定金。 客户可以在产品页面上选择是支付 10% 的定金还是支付订单的全额。运输
我有这个 Lua 脚本,它应该创建一个新类,创建一个实例并调用函数,但是在我实际调用方法时出现错误。 Account = { balance = 0, new = function(s
我是一名优秀的程序员,十分优秀!