0

我是一个初学者程序员,正在编写一个模拟银行的程序来跟踪各种银行账户(我不知道我在做什么,只是把它放在那里。)练习我新获得的面向对象编程知识. 当我运行我的程序时,我的 Manager 类中创建帐户并将它们存储在列表中的部分出现错误。我将在下面发布整个源代码以及错误。随意更正您认为不正确的任何其他内容,我一直在寻找改进代码的方法。

# 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()


Menu()

这是错误。在 AccManager 的 create_account 方法实例化一个新帐户后,我收到错误消息。

TypeError: create_account() takes at least 2 non-keyword positional arguments (0 given)
4

2 回答 2

0

您定义create_account为实例方法,但将其称为类方法。尝试将其更改为:

@classmethod
def create_account(cls, ID, bal=0):
    ...

不过,您可能需要保留在某处创建的帐户?

于 2013-03-25T22:29:25.760 回答
0

AccManager.create_account是一个未绑定的方法,即它不属于任何对象。因此,当您调用它时,它需要参数

(self, ID, bal = 0)

但是,您在没有值的情况下调用self

AccManager.create_account(ID = id_choice,bal = bal_choice)

你想创建一个 AccManager 对象,然后获取它的 create_account方法:

am = AccManager() # Create object
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): "))
            am.create_account(ID = id_choice,bal = bal_choice)
            Menu()
于 2013-03-25T22:30:46.903 回答