运行此代码时出现以下错误:
属性错误:DisplayWelcome没有属性'completeKey'
import controller.game_play
import cmd
class DisplayWelcome(cmd.Cmd):
"""Welcome user to game"""
def __init__(self):
self.do_greet()
prompt = 'ENTER'
intro = '\n'.join([' Welcome To ',
'...ZOMBIE IN MY POCKET...'])
def do_greet(self):
print (' Welcome To ')
print ("...ZOMBIE IN MY POCKET...")
def do_inform(self, line):
k = input('Enter a letter')
print (k)
def main():
d = DisplayWelcome()
#d.do_greet()
d.cmdloop()
s = controller.game_play.stuff()
if __name__ == '__main__':
main()
答案 0 :(得分:16)
这很容易...... ;-) 您忘了调用父类的构造函数(cmd.Cmd)。在那里, completekey 属性会自动声明为默认值。这解决了问题!
import controller.game_play
import cmd
class DisplayWelcome(cmd.Cmd):
"""Welcome user to game"""
def __init__(self):
#### THIS IS THE LINE YOU FORGOT!!!!
super(DisplayWelcome, self).__init__()
# or cmd.Cmd.__init__(self)
self.do_greet()
prompt = 'ENTER'
intro = '\n'.join([' Welcome To ',
'...ZOMBIE IN MY POCKET...', ' Created by Ben Farquhar '])
def do_greet(self):
print (' Welcome To ')
print ("...ZOMBIE IN MY POCKET...")
print (" Created by Ben Farquhar ")
def do_inform(self, line):
k = input('Enter a letter')
print (k)
def main():
d = DisplayWelcome()
#d.do_greet()
d.cmdloop()
s = controller.game_play.stuff()
if __name__ == '__main__':
main()