如果在 class 中调用,函数不会 return 任何值
Function does not return any value if called in a class
我正在尝试根据我找到的教程制作我的 Zork & Adventures 版本 here
本教程使用引擎 class 从地图 class select 接下来必须出现什么场景等等。
对于 select 下一个场景,引擎采用函数的 return 值和要播放的场景 select。
从系统导入退出
来自随机导入 randint
class Scene(object):
def enter(self):
print "This scene is not yet configured. Subclass it and implement enter()."
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
# be sure to print out the last scene
current_scene.enter()
class Death(Scene):
quips = [
"You died. You kinda suck at this.",
"Your mom would be proud...if she were smarter.",
"Such a luser.",
"I have a small puppy that's better at this."
]
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class CentralCorridor(Scene):
def enter(self):
print "you entered"
action = raw_input("> ")
if action == "1":
return 'death'
elif action == "2":
return 'death'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
class Finished(Scene):
def enter(self):
print "You won! Good job."
return 'finished'
class Map(object):
scenes = {
'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod(),
'death': Death(),
'finished': Finished(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
我讨厌这段代码的地方在于,如果用户拼错 action
,class 会重新开始。我不想再次打印所有内容,我只想让用户收到 raw_input
.
提示
我认为这就足够了:
class Actions(object):
# Asks a command to the user
def action(self, actions):
self.actions = actions
command = raw_input('> ')
if command in self.actions.keys():
return self.actions[command]
elif command == 'HELP':
print 'The available actions are:'
for value in self.actions.keys():
print ' * ', value
self.action(self.actions)
else:
print 'Repeat please:'
self.action(self.actions)
cmd = Actions()
有了这个模块,用户可以数字他想要的东西,直到他数字的动作是字典中的动作之一。
当我 运行 这个模块单独工作时。如果我键入 print
而不是 return
,它将打印该值。
但是,如果我导入此模块,除了 return 值:
之外,一切都会正常工作
class CentralCorridor(Scene):
actions = {
'quit': 'death' # I need that cmd.action() returns 'death'
}
def enter(self):
print "you entered"
cmd.action(CentralCorridor.actions)
python控制台returns:
Traceback (most recent call last):
File "main.py", line 77, in <module>
a_game.play()
File "main.py", line 24, in play
next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
看起来它只适用于这段代码(我不想使用的代码):
动作 = raw_input("> ")
if action == "1":
return 'death'
elif action == "2":
return 'death'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
你能解释一下我做错了什么吗?为什么它不起作用或如何让它起作用?
这是因为您没有return您在操作中递归调用的值class,它应该是:
class Actions(object):
# Asks a command to the user
def action(self, actions):
self.actions = actions
command = raw_input('> ')
if command in self.actions.keys():
return self.actions[command]
elif command == 'HELP':
print 'The available actions are:'
for value in self.actions.keys():
print ' * ', value
return self.action(self.actions) # return recursive call
else:
print 'Repeat please:'
return self.action(self.actions) # return here too
cmd = Actions()
您还忘记了 return CorridorScene 中的 cmd.action 调用:
class CentralCorridor(Scene):
actions = {
'quit': 'death' # I need that cmd.action() returns 'death'
}
def enter(self):
print "you entered"
return cmd.action(CentralCorridor.actions) # return here too
我正在尝试根据我找到的教程制作我的 Zork & Adventures 版本 here
本教程使用引擎 class 从地图 class select 接下来必须出现什么场景等等。 对于 select 下一个场景,引擎采用函数的 return 值和要播放的场景 select。
从系统导入退出 来自随机导入 randint
class Scene(object):
def enter(self):
print "This scene is not yet configured. Subclass it and implement enter()."
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
last_scene = self.scene_map.next_scene('finished')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
# be sure to print out the last scene
current_scene.enter()
class Death(Scene):
quips = [
"You died. You kinda suck at this.",
"Your mom would be proud...if she were smarter.",
"Such a luser.",
"I have a small puppy that's better at this."
]
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class CentralCorridor(Scene):
def enter(self):
print "you entered"
action = raw_input("> ")
if action == "1":
return 'death'
elif action == "2":
return 'death'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
class Finished(Scene):
def enter(self):
print "You won! Good job."
return 'finished'
class Map(object):
scenes = {
'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod(),
'death': Death(),
'finished': Finished(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_name):
val = Map.scenes.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
我讨厌这段代码的地方在于,如果用户拼错 action
,class 会重新开始。我不想再次打印所有内容,我只想让用户收到 raw_input
.
我认为这就足够了:
class Actions(object):
# Asks a command to the user
def action(self, actions):
self.actions = actions
command = raw_input('> ')
if command in self.actions.keys():
return self.actions[command]
elif command == 'HELP':
print 'The available actions are:'
for value in self.actions.keys():
print ' * ', value
self.action(self.actions)
else:
print 'Repeat please:'
self.action(self.actions)
cmd = Actions()
有了这个模块,用户可以数字他想要的东西,直到他数字的动作是字典中的动作之一。
当我 运行 这个模块单独工作时。如果我键入 print
而不是 return
,它将打印该值。
但是,如果我导入此模块,除了 return 值:
class CentralCorridor(Scene):
actions = {
'quit': 'death' # I need that cmd.action() returns 'death'
}
def enter(self):
print "you entered"
cmd.action(CentralCorridor.actions)
python控制台returns:
Traceback (most recent call last):
File "main.py", line 77, in <module>
a_game.play()
File "main.py", line 24, in play
next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'
看起来它只适用于这段代码(我不想使用的代码): 动作 = raw_input("> ")
if action == "1":
return 'death'
elif action == "2":
return 'death'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
你能解释一下我做错了什么吗?为什么它不起作用或如何让它起作用?
这是因为您没有return您在操作中递归调用的值class,它应该是:
class Actions(object):
# Asks a command to the user
def action(self, actions):
self.actions = actions
command = raw_input('> ')
if command in self.actions.keys():
return self.actions[command]
elif command == 'HELP':
print 'The available actions are:'
for value in self.actions.keys():
print ' * ', value
return self.action(self.actions) # return recursive call
else:
print 'Repeat please:'
return self.action(self.actions) # return here too
cmd = Actions()
您还忘记了 return CorridorScene 中的 cmd.action 调用:
class CentralCorridor(Scene):
actions = {
'quit': 'death' # I need that cmd.action() returns 'death'
}
def enter(self):
print "you entered"
return cmd.action(CentralCorridor.actions) # return here too