我是一名初级程序员,正在构建一个程序,该程序模拟具有多个银行账户的银行,用户可以提取/存入现金、创建账户、获取汇率等。目前,我正在尝试访问一组实例我的班级都是我的帐户班级的对象。Account Manager 类负责管理这些帐户对象并在需要用户输入时帮助组织它们。现在,我正在尝试在我的菜单上模拟我的第三个选项,该选项获取有关用户选择的帐户的信息(他们必须手动输入其帐户的 ID 以检索其信息、提取/存入现金等.)。尽管我已经设法将所有这些类实例存储在一个列表中,但我似乎无法使用我的 get_account 方法来检索这些以供使用。我将在下面发布我的所有代码。如果您发现其他任何不合适的地方,请随时告诉我。代码:
# Virtual Bank
# 3/21/13
# Account Manager Class
class AccountManager(object):
"""Manages and handles accounts for user access"""
# Initial
def __init__(self):
self.accounts = []
# create account
def create_account(self, ID, bal = 0):
# Check for uniqueness? Possible method/exception??? <- Fix this
account = Account(ID, bal)
self.accounts.append(account)
def get_account(self, ID):
for account in self.accounts:
if account.ID == ID:
return account
else:
return "That is not a valid account. Sending you back to Menu()"
Menu()
class Account(object):
"""An interactive bank account."""
wallet = 0
# Initial
def __init__(self, ID, bal):
print("A new account has been created!")
self.id = ID
self.bal = bal
def __str__(self):
return "|Account Info| \nAccount ID: " + self.id + "\nAccount balance: $" + self.bal
# Main
AccManager = AccountManager()
def Menu():
print(
"""
0 - Leave the Virtual Bank
1 - Open a new account
2 - Get info on an account
3 - Withdraw money
4 - Deposit money
5 - Transfer money from one account to another
6 - 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":
id_choice = input("What would you like your account to be named?: ")
bal_choice = float(input("How much money would you like to deposit?(USD): "))
AccManager.create_account(ID = id_choice,bal = bal_choice)
Menu()
elif choice == "2":
acc_choice = input("What account would you like to access?(ID only, please): ")
AccManager.get_account(acc_choice)
print(acc_choice)
Menu()