如果在类中调用,函数不会返回任何值

时间:2015-06-01 17:51:48

标签: python function class methods

我正在努力制作我的Zork&我发现here

教程中的冒险经历

本教程使用一个Engine类,它从Map类中选择下一个必须进入的场景,依此类推。 要选择下一个场景,引擎将获取一个函数的返回值并选择要播放的场景。

来自sys import exit的

来自随机导入randint

 include_once 'wp-config.php';
    $number = 14 ;
    $result = mysql_query ("SELECT * FROM 'wp_posts' WHERE post_status='publish' ORDER BY ID DESC LIMIT $number 14 ); 
    while ($row = mysql_fetch_array($result)) 
  {
 $id = $row["ID"];
 $title = $row["post_title"];
 $content = $row["post_content"];

我讨厌此代码的原因是,如果用户拼错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

我认为这就足够了:

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

python控制台返回:

class CentralCorridor(Scene):

    actions = {
        'quit': 'death' # I need that cmd.action() returns 'death'
    }

    def enter(self):
        print "you entered"

        cmd.action(CentralCorridor.actions)

它看起来只适用于此代码(我不想使用的代码):     action = raw_input(">")

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'

你能否解释一下我做错了什么以及为什么它不能工作或如何使它发挥作用?

1 个答案:

答案 0 :(得分:1)

这是因为你没有在Actions类中返回递归调用的值,它应该是:

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()

你也忘了在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