0

I'm a beginner programmer who is writing a program that simulates a bank that keeps track of various bank accounts (I have no idea what I'm doing, just putting that out there.) to practice my newly-acquired object oriented programming knowledge. When I run my program, the part of my Manager class that creates accounts and stores them in a list gets an error. I'll post the entire source code below, along with the error. Feel free to correct anything else that you think is off, I'm always looking for ways to improve my code.

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

This is the error. I get the error after AccManager's create_account method instantiates a new account.

TypeError: create_account() takes at least 2 non-keyword positional arguments (0 given)
hasherr
  • 669
  • 3
  • 14
  • 28

2 Answers2

0

You define create_account as an instance method, but call it as a class method. Try changing it to this:

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

Though, you might need to retain the account you created somewhere?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I'm pretty sure he wants to retain the account in an `AccountManager` object. – phihag Mar 25 '13 at 22:33
  • What is a class method? It doesn't describe it in my book, and I can't seem to find a clear definition of it. But, I am the idiot who doesn't know what a static method is either, so just ignore me if you want. :) – hasherr Mar 25 '13 at 22:43
0

AccManager.create_account is an unbound method, i.e. it doesn't belong to any object. Therefore, when you call it, it expects the arguments

(self, ID, bal = 0)

However, you call it without a value for self in the line

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

You want to create an AccManager object, and then get its create_account method:

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()
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Actually, I realize that I forgot the () when I declared AccManager. Thanks for the help though. – hasherr Mar 25 '13 at 22:41