簡體   English   中英

如何檢查輸入是否等於某個整數或多個整數?

[英]How do you check to see if an input is equal to a certain integer or integers?

我知道如何檢查輸入是否為整數而不是特定整數。 例如,對於我的代碼,我要檢查,如果輸入等於1,2或3,然后再要求用戶輸入如果輸入不等於 1,2或3。

非常感謝您的幫助:)

那將是:

if var in (1, 2, 3):

或對於任何整數:

if isinstance(var, int):
def restricted_input(prompt, values):
    assert values
    while True:
        typed = input(prompt)
        for acceptable in values:
            if typed == str(acceptable):
                return acceptable
        else:
            print("Acceptable values are from ", values)

然后在您的程序中...

choice = restricted_input("Enter a number: ", {1, 2, 3})

暫無
暫無

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

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