0

我是一名初级程序员,正在构建一个程序,该程序模拟具有多个银行账户的银行,用户可以提取/存入现金、创建账户、获取汇率等。目前,我正在尝试访问一组实例我的班级都是我的帐户班级的对象。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()
4

2 回答 2

3

您的Account对象实际上似乎没有ID属性;相反,它们具有id属性。Python 区分大小写;尝试更改if account.ID == IDif account.id == ID.

编辑:

在第一次不匹配之后,您也会返回。您需要从块中删除一级缩进,else以便首先通过整个循环,事实上,您的else块甚至不应该是else块,因为您实际上并没有匹配if; 只有在没有帐户与给定 ID 匹配时,该方法才会失败。

编辑2:

此外,您实际上并没有将返回值分配get_account()给任何东西,因此它丢失了。我不确定你期望在那里发生什么。

于 2013-03-26T00:40:24.237 回答
0

错误出现在第 31 和 35 行。您写的是“id”而不是“ID”。充分利用这两件事:

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

请让我们知道代码在此之后是否有效。

于 2013-03-26T02:06:25.077 回答