我目前正在尝试从头开始构建一个简单的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()
答案 0 :(得分:0)
foo = Atm()
将1个参数传递给Atm.__init__
- 隐式self
。其他两个参数(acctId
和acctPass
)都没有了,所以python抱怨。
在我看来,你可以一起摆脱__init__
并绑定greetMenu
中的实例属性:
class Atm:
acctPass = 0
acctID = 0
def greetMenu(self):
while self.acctPass == 0 or self.acctID == 0:
print "Please enter a password and your account number to proceed: "
self.acctpass = raw_input("Password: ")
self.acctID = int(raw_input("Account Number: "))
self.mainMenu()
# etc.
在这里,您可能仍需要使用mainMenu
来使其工作(请注意,现在我们没有通过函数调用参数传递参数 - 值存储在类)。
答案 1 :(得分:0)
这是使用foo = ATM()
def __init__(self, acctID=0, acctPass=0):
将=0
添加到参数会将它们初始化为0
现在你已经覆盖了构造函数以接受1,2或3个值。
在greetmenu
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
您需要将参数发送到函数ATM.greetmenu(1234,'pwd')
或使用类中定义的参数。
def greetMenu(self):
while self.acctPass == 0 or self.acctID == 0:
print "Please enter a password and your account number to proceed: "
self.acctpass = raw_input("Password: ")
self.acctID = raw_input("Account Number: ")
foo.mainMenu()
#return acctPass, acctID # first step to transfer data between two functions