学习Python艰难之路ex 43循环问题

时间:2015-03-30 01:13:55

标签: python class object while-loop

我目前正在从“学习Python艰难的方式”开始这项练习,并且正在努力解决一些问题。每当我做到的时候 我的代码的LaserWeaponArmory(scene)部分,它将在执行实际死亡场景之前循环两次。基本上允许我20次尝试猜测门代码而不是10次。它将在接下来的10次尝试之前循环回到场景的开始。之后,它将正常退出。

对象和类开始对我更有意义,但我只是在寻找解决此问题的方向的一些指示。我很感激任何人提供的建议!

我在下面的问题中提供了我认为相关代码的相关代码,但我仍然是n00b,所以如果您需要查看更多内容来回答我的问题,请告诉我们!

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 kind 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 LaserWeaponArmory(Scene):

    def enter(self):
        print "You do a dive roll into the Weapon Armory, crouch and scan the room"
        print "for more Gothons that might be hiding. It's dead quiet, too quiet."
        print "You stand up and run to the far side of the room and find the"
        print "neutron bomb and it's container. There's a keypad lock on the box"
        print "and you need the code to get the bomb out. If you get the code"
        print "wrong 10 times then lock closes forever and you can't"
        print "get the bomb. The code is 3 digits."
        code = "%d%d%d" % (randint(1, 9), randint(1,9), randint(1,9))
        guesses = 0
        attempt = 1
        print "Attempt number %s." % attempt
        guess = raw_input("[keypad]> ")


        while guess != code and guesses != 9:
            print "BZZZZZZEDDD!"
            guesses += 1
            attempt += 1
            print "Attempt number %s." % attempt
            guess = raw_input("[keypad]> ")

        if guess == code:
            print "The container clicks open and the seal breaks, letting gas out."
            print "You grab the neutron bomb and run as fast as you can to the"
            print "bridge where you must place it in the right spot."
            return 'the_bridge'
        else:
            print "The lock buzzes one last time and then you hear a sickening"
            print "melting sound as the mechanism is fused together."
            print "You decide to sit there, and finally the Gothons blow up the"
            print "ship from their ship and you die."
            return 'death'

1 个答案:

答案 0 :(得分:3)

Engine循环中,每个场景enter两次。第一次是最后一行:

current_scene.enter()

第二次是你重新进入循环时:

next_scene_name = current_scene.enter()

我认为最后一行应该在while循环之外。只是取消缩进:)