我是一名对计算机科学感兴趣的高中初级程序员。我一直在使用 Mike Dawson 的书自学 Python编程,Learning to Programming for the absolute 初学者 with Python, 3rd Edition. 我正在编写一个程序来练习我新获得的 OOP 知识。该程序模拟银行,玩家可以在其中开设银行账户、提取/存入现金、获取汇率、将资金从一个账户转移到另一个账户以及其他功能。我在程序中遇到了严重的问题,因为程序的主要部分模拟了用户可以访问的多个可访问帐户这一事实。我现在确定我应该如何或是否应该这样做,但我尝试编写一个名为 all_accounts 的空列表的类属性。每次实例化帐户时,都会将该实例附加到列表中。当玩家选择诸如提取/存入现金之类的菜单选项时,我希望访问列表中的实例,以便可以修改对象的属性(例如余额)。我不知道该怎么做,我一直在寻找答案大约 3 天,但没有找到任何东西。请记住,我是一个初学者程序员,所以我的代码可能会被认为写得不好。我将在下面发布整个代码,因为我认为人们有必要全面了解代码的工作原理和功能。此外,如果我的代码中有任何其他不正确或我可以做得更好的地方,请随时通知我。我总是乐于学习。如果我的代码中有任何其他不正确或我可以做得更好的地方,请随时通知我。我总是乐于学习。如果我的代码中有任何其他不正确或我可以做得更好的地方,请随时通知我。我总是乐于学习。
class Account(object):
"""An interactive bank account"""
wallet = 0
all_accounts = []
# initial
def __init__(self, ID, bal):
self.ID = ID
self.bal = bal
print("A new account has been opened!")
Account.all_accounts.append(self)
def withdraw(self, w_input):
if w_input < self.bal or w_input == 0:
print("That is not a valid amount. Sending you back to menu.\n")
Menu()
else:
self.bal = self.bal - w_input
print("You withdrew $" + str(w_input) + ".\n")
wallet += w_input
print("You wallet now contains $" + Account.wallet)
Menu()
def deposit(self):
# Whatever
def Menu():
print(
"""
0 - Leave the Virtual Bank
1 - Open a new account
2 - Withdraw money
3 - Deposit money
4 - Transfer money from one account to another
5 - Get exchange rates(Euro, Franc, Pounds, Yuan, Yen)
"""
) # Add more if necessary
choice = input("What would you like to do?: ")
while choice != "0":
if choice == "1":
account_name = input("What is your account's ID?: ")
account_bal = float(input("How much money would you like to put into the account?(USD): "))
account_name = Account(ID = account_name, bal = account_bal)
Menu()
elif choice == "2":
account_choice = input("What is your account ID?: ")
if account_choice in instance.Account.all_account:
withdraw_choice = input("How much money would you like to withdraw?: ")
account_choice.withdraw(w_input = withdraw_choice)
else:
print("Nope.")
if choice == "0":
print("\nThank you for visiting the virtual bank!")
input("Press the [ENTER] key to exit the program.")
Menu()