0

我这里只有一个小程序,我想用它来接受用户的输入(就像输入密码一样)。我有一个包含密码的列表,然后我将用户输入并将其放在一个空类中。然后将该类与其中包含密码的类进行比较,如果匹配,它将返回“好”。但是,我只能使用一位数来做到这一点。如何允许用户使用多个整数?这是做这类事情的有效方法吗?有没有更快更有效的方法?谢谢。

class KeyCode(object):


    def access(self):
        room_code = [1]
        print "Before you enter you must provide the room code: "
        attempt = []
        code = int(raw_input('>>'))
        attempt.append(code)
        if attempt == room_code:
             print "Good"
        else: 
             return 'death'

class Boxing_room(KeyCode):


    def enter(self): 
        print "This is the boxing studio"

        return 'Gymnast_room'
4

3 回答 3

1

不一定需要列表。您可以只比较字符串,或者如果您的代码只是数字,整数。

此外,这里的课程并没有真正的帮助(除非在这里只是为了了解它们)。一个函数就足够了:

def access():
    room_code = 12534
    code = int(raw_input('Enter the code: '))
    if code == room_code:
        return 'good'
    return 'death'
于 2013-09-12T21:51:04.090 回答
1

您可以使用字典来存储键码:

code_dict = {'Boxing':'12345', 'Locker':'00000'}

并测试

if code_input == code_dict['Boxing']:
    ...
于 2013-09-13T18:49:53.563 回答
0

我同意 Haidro 的回答,但在我看来,您可能希望允许多个密码?

如果是这种情况,您只需要进行“IN”检查。

例如。

def access():
    room_code = [12345,54321]
    code = int(raw_input('Enter the code: '))
    if code in room_code:
        return 'good'
    return 'death'
于 2013-09-12T22:22:34.343 回答