簡體   English   中英

添加從用戶輸入中獲取的多個數字並將其附加到列表中

[英]Add multiple numbers taken from user input and append it to a list

我只是在這里有一個小程序,我想用來從用戶那里獲取輸入(就像輸入密碼一樣)。 我有一個包含密碼的列表,然后我將用戶輸入並將其放在一個空類中。 然后將該類與其中包含密碼的類進行比較,如果匹配,則返回“good”。 但是我只能用一位數來做到這一點。 如何允許用戶使用多個整數? 這是一種做這種事情的有效方式嗎? 有更快更有效的方法嗎? 謝謝。

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'

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

此外,一堂課在這里並不是真正有用(除非它只是為了了解它們)。 一個函數就足夠了:

def access():
    room_code = 12534
    code = int(raw_input('Enter the code: '))
    if code == room_code:
        return 'good'
    return 'death'

您可以使用字典來存儲密鑰代碼:

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

並測試

if code_input == code_dict['Boxing']:
    ...

我同意Haidro的回答,但在我看來你可能想要允許多個密碼?

如果是這種情況,您只需要進行“IN”檢查。

例如。

def access():
    room_code = [12345,54321]
    code = int(raw_input('Enter the code: '))
    if code in room_code:
        return 'good'
    return 'death'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM