学习Python中的randint用法艰难的练习43

时间:2013-11-13 17:43:51

标签: python python-2.7 python-import

我在学习python中练习43的randint使用困难link to exercise。假设我在程序的所有其他部分完全遵循Zed Shaw的代码,并且我有from random import randint,当我运行程序并在键盘中输入3位数密码时,它返回“BZZZZEDDD!”。这是代码部分:

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 in its 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 the 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))
        guess = raw_input("[keypad]> ")
        guesses = 0

        while guess != code and guesses < 10:
            print "BZZZZEDDD!"
            guesses += 1
            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'

让我们在guess = raw_input("[keypad]> ")运行程序时输入“368”。 不应该在code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))的参数范围内,if guess ==code:为TRUE吗?相反,它运行它就好像猜!=代码并返回“BZZZZEDDD!”

1 个答案:

答案 0 :(得分:0)

您对368的猜测在代码的可能范围内,但这不是while循环检查的内容。这条线

code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))

将生成一个包含三个随机数字的字符串。代码可以是111到999之间的任何值(除了没有零),你无法确切知道程序当前的含义。在本课程的最后,在Study Drills下,作者说:

  
      
  1. 为游戏添加作弊码,以便您可以越过困难   客房。我可以在一行中用两个单词来做到这一点。
  2.   

据推测,这段代码是他正在讨论的房间之一。尝试添加一些可以提示代码的东西。