我目前正在尝试从头开始构建一个简单的ATM程序(基于文本)。我的问题是在函数之间传递用户输入的变量。我得到的错误是(init()正好接受3个参数(给定1个参数))。有人能解释一下发生了什么,我做错了什么吗
class Atm:
acctPass = 0
acctID = 0
def __init__(self, acctID, acctPass):
#self.acctName = acctName
#self.acctBal = acctBal
self.acctPass = acctPin
self.acctID = acctID
def greetMenu(self, acctID, acctPass):
while acctPass == 0 or acctID == 0:
print "Please enter a password and your account number to proceed: "
acctpass = raw_input("Password: ")
acctID = raw_input("Account Number: ")
foo.mainMenu()
return acctPass, acctID # first step to transfer data between two functions
def mainMenu(self, acctID, acctPass):
print ""
acctpass = foo.preMenu(acctPass, acctID)
print acctPass
print "Made it accross!"
def deposit():
pass
def withdrawl():
pass
foo = Atm()
foo.greetMenu()
这是用
foo = ATM()
调用的构造函数将
=0
添加到参数会将它们初始化为0 现在您已经重写了构造函数以接受1、2或3个值在greetmenu中
您需要要么将参数发送到函数
ATM.greetmenu(1234,'pwd')
,要么像这样使用类中定义的参数只将1个参数传递给
Atm.__init__
隐式self
。其他两个参数(acctId
和acctPass
)丢失,因此python会抱怨在我看来,您可以一起摆脱
__init__
,并绑定greetMenu
中的实例属性:在这里,您可能仍然需要对
mainMenu
进行一些处理才能使其正常工作(注意,现在我们不是通过函数调用参数传递参数值存储在类上)相关问题 更多 >
编程相关推荐