使用带有while循环的字典

Cha*_*ith 2 python dictionary while-loop

我试图在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!"
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 5

不要求guess在循环之前,因为如果答案是正确的 - 它根本不会进入循环.更换:

guess = int(raw_input('What is the code? >>'))
Run Code Online (Sandbox Code Playgroud)

有:

guess = None
Run Code Online (Sandbox Code Playgroud)