2017-04-07 52 views
-2

試圖修復一些代碼:故障與如果和多個整數

function(): 
    answer = int(input("Write a number")) 
    if answer == 101: 
     print("You found a secret!") 
     do_stuff() 
    elif answer >= 5 and answer <= 100 and answer >= 102: 
     print("This is not a valid option!") 
     function() 
    else: 
     continue with other inputs 

這是專門的elif的答案> = 5(等)是不正常的,它只是轉移到了別的:其他投入,我不知道如何解決它。

+4

你什麼時候認爲'答案'小於100 **而**大於102? –

+0

一個數字不能同時在5到100之間,也大於102! – sgrg

+1

我懷疑你只是指'elif answer> = 5',因爲第一個案例已經排除了'101'。 – khelwood

回答

3

它永遠不會出現的情況是

answer >= 5 and answer <= 100 and answer >= 102 

!我想你可能的意思是

answer >= 5 and answer <= 100 or answer >= 102 

。或者如何:

5 <= answer <= 100 or 102 <= answer 
+0

(注意''和'和'或'之間的區別) –

1

對於你第一次elif語句,你的邏輯是不正確的。 python和聲明意味着所有條件必須是真實的才能執行。然而,你寫的是答案必須大於5,小於100,大於102.這是沒有意義的,因爲數字不能大於100而小於100.你需要的是聲明:

elif answer >= 5 and answer <= 100 or answer >= 102: