Python 为什么我的类方法得到2个非关键字位置关键字参数TypeError?
我是一个初学者程序员,正在编写一个程序,模拟一家银行,跟踪各种银行账户(我不知道我在做什么,只是把它放在那里)来实践我新学到的面向对象编程知识。当我运行程序时,Manager类中创建帐户并将其存储在列表中的部分会出错。我将在下面发布整个源代码以及错误。请随意更正您认为不合适的任何内容,我一直在寻找改进代码的方法Python 为什么我的类方法得到2个非关键字位置关键字参数TypeError?,python,oop,python-3.x,Python,Oop,Python 3.x,我是一个初学者程序员,正在编写一个程序,模拟一家银行,跟踪各种银行账户(我不知道我在做什么,只是把它放在那里)来实践我新学到的面向对象编程知识。当我运行程序时,Manager类中创建帐户并将其存储在列表中的部分会出错。我将在下面发布整个源代码以及错误。请随意更正您认为不合适的任何内容,我一直在寻找改进代码的方法 # Virtual Bank # 3/21/13 # Account Manager Class class AccountManager(object): """Manage
# 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()
您将
create\u account
定义为实例方法,但将其称为类方法。尝试将其更改为:
@classmethod
def create_account(cls, ID, bal=0):
...
不过,您可能需要保留在某处创建的帐户?
AccManager。创建帐户是一种方法,即它不属于任何对象。因此,当您调用它时,它需要参数
(self, ID, bal = 0)
但是,您调用它时,行中没有self
的值
AccManager.create_account(ID = id_choice,bal = bal_choice)
您希望创建AccManager对象,然后获取其create\u 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()
我很确定他想在AccountManager
对象中保留帐户。什么是类方法?我的书中没有描述它,我似乎也找不到一个明确的定义。但是,我是一个也不知道静态方法是什么的白痴,所以如果你想的话,请忽略我。:)实际上,我意识到我在声明AccManager时忘记了()。谢谢你的帮助。
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()