Skip to main content
 首页 » 编程设计

python之在 while 循环中重置 raw_input (python)

2025年05月04日1398°冷暖

所以我试图让这个引擎工作,我做到了,但是它破坏了我的程序。 (来自 LPTHW)

我基本上是在尝试访问一个函数,它从用户那里获得 11 次输入,如果用户未能猜出正确的输入,他们就会死(在游戏中),但我修复了将提示从引擎发送到函数,似乎破坏了我得到 11 次输入的函数,并且对所有 11 个输入都使用相同的猜测。

这是主引擎

globvar = ' ' 
 
class Game(object): 
 
    def __init__(self, start): 
        self.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." 
        ] 
        self.start = start 
 
 
    def play(self): 
        # next_room_name is taken from init argument start 
        next_room_name = self.start 
 
        while True: 
            global globvar 
            print "\n--------" 
            # set variable room to get function from next_room_name 
            room = getattr(self, next_room_name) 
 
            print room.__doc__ 
 
            if room == self.laser_weapon_armory: 
                prompt = raw_input("[keypad]> ") 
 
            elif room == self.escape_pod: 
                prompt = raw_input("[pod #]> ") 
            else: 
                prompt = raw_input("> ") 
        globvar = prompt      
            # unpacks function from next_room_name into room 
            next_room_name = room() 

这是我要开始工作的功能

def laser_weapon_armory(self): 
        """ 
        You do a dive roll into the Weapon Armory, crouch and scan the room 
        for more Gothons that might be hiding.  It's dead quiet, too quiet. 
        You stand up and run to the far side of the room and find the 
        neutron bomb in its container.  There's a keypad lock on the box 
        and you need the code to get the bomb out.  If you get the code 
        wrong 10 times then the lock closes forever and you can't 
        get the bomb.  The code is 3 digits. 
        """ 
        code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9)) 
 
        guess = globvar 
        guesses = 0 
 
        while guess != code and guesses < 10: 
            print "BZZZZEDDD!" 
            guesses += 1 
            guess = globvar 
 
        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' 

这是我得到的输出

-------- 
 
        You do a dive roll into the Weapon Armory, crouch and scan the room 
        for more Gothons that might be hiding.  It's dead quiet, too quiet. 
        You stand up and run to the far side of the room and find the 
        neutron bomb in its container.  There's a keypad lock on the box 
        and you need the code to get the bomb out.  If you get the code 
        wrong 10 times then the lock closes forever and you can't 
        get the bomb.  The code is 3 digits. 
 
[keypad]> 124 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
BZZZZEDDD! 
The lock buzzes one last time and then you hear a sickening 
melting sound as the mechanism is fused together. 
You decide to sit there, and finally the Gothons blow up the 
ship from their ship and you die. 

请您参考如下方法:

这是您正在寻找的东西吗?你的问题好像不是很清楚

while guess != code and guesses < 10: 
        guess = raw_input("BZZZZEDDD! - try again?") 
        guesses += 1 
        guess = ''