【发布时间】:2026-01-04 03:15:03
【问题描述】:
我的程序创建了一个 Account 对象,其帐户 ID 为 1122,余额为 20,000 美元,年利率为 4.5%。使用withdraw方法取出$2,500,deposit方法存入$3,000,打印id、余额、月利率、月利息。
我在下一部分遇到了问题,即我的程序还应该询问 id、余额和利率的起始值。然后它应该显示一个菜单,用户可以在其中修改他们的帐户。每次选择后,应显示适当的消息。然后应该会再次显示菜单。
示例菜单:
(1):显示 ID
(2):显示平衡
(3):显示年利率
(4):显示月利率
(5):显示每月利息
(6):取款
(7):存钱
(8):退出
如何修改我的代码以在用户输入的情况下正常运行?现在它正在工作,但我没有任何要求用户输入的代码。
这是我的代码:
main module:
from Account import Account
def main():
updatedAccount = Account(1122,20000,4.5)
updatedAccount.withdraw(2500)
print("User ID : ", updatedAccount.id)
print("Beginning Balance: ", updatedAccount.balance)
print("Monthly Interest Rate: ", updatedAccount.monthly_interest_rate)
print("Monthly Interest: ", updatedAccount.get_monthly_interest())
main()
帐户.py
class Account:
def __init__(self, id, initial_balance=0, rate=4.5):
self.id = id
self.balance = initial_balance
self.annual_interest_rate = rate
@property
def monthly_interest_rate(self):
return self.annual_interest_rate / 12
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
我已尝试为起始帐户值编写用户输入,但在使用它来创建帐户对象以及如何实现菜单时遇到了麻烦。
userid = float(input("Enter User ID: "))
InterestRate = float(input("Enter Interest Rate: "))
balance = float(input("Enter balance: "))
print("User ID is: ", userid)
print("Interest Rate: ", InterestRate)
print("Balance is: ", balance)
【问题讨论】:
-
基本上你需要,在一个循环中,(1) 显示菜单选项,(2) 阅读用户的选择,(3) 做用户要求的事情。这是相当广泛的——如果出现更具体的问题,或者如果您有菜单代码并且可以明确指出其行为与您想要的行为有何不同,我建议您尝试上述方法并编辑帖子。
标签: python python-3.x