2

我试图在 while-loops 条件语句中使用字典键,程序不会将任何输入识别为“正确”

class Gymnast_room(KeyCode):    
    def enter(self):
        print "This is the gymnastics room, we got foam pits and rings!"
        print "Before you can enter, you have to enter the code"
        guess = int(raw_input('What is the code? >>'))

        passcodes = {'Gymnast_code': 1234,
        'Powerlifting_code': 1234
        }
        while guess != passcodes['Gymnast_code']:
            guess = int(raw_input('What is the code? >>'))
            if guess == passcodes['Gymnast_code']:
                print "Correct!"
                return Powerlifting_room()
            else:
                print "Incorrect!"
4

3 回答 3

5

不要guess在循环之前询问,因为如果答案是正确的 - 它根本不会进入循环。代替:

guess = int(raw_input('What is the code? >>'))

和:

guess = None
于 2013-09-20T20:58:07.277 回答
2

尝试这个:

def enter(self):
    print "This is the gymnastics room, we got foam pits and rings!"
    print "Before you can enter, you have to enter the code"
    guess = int(raw_input('What is the code? >>'))

    passcodes = {'Gymnast_code': 1234,
    'Powerlifting_code': 1234
    }
    while guess != passcodes['Gymnast_code']:
        print "Incorrect!"
        guess = int(raw_input('What is the code? >>'))

    print "Correct!"
    return Powerlifting_room()
于 2013-09-20T20:59:17.533 回答
1

在这里,您说“虽然密码不是体操运动员代码,但检查它是否是业务代码”,因为当您到达这里时,它永远不会进入 while 循环并且永远不会完成

   while guess != passcodes['Gymnast_code']:
        guess = int(raw_input('What is the code? >>'))
        if guess == passcodes['Gymnast_code']:
            print "Correct!"
            return Powerlifting_room()
        else:
            print "Incorrect!"
于 2013-09-20T20:58:33.243 回答