2015-09-13 114 views
-1

現在問題來自我的if語句,即使我鍵入「是」,它仍然會說「無效輸入」並重新執行我的main()函數。不知道什麼是錯誤的。不確定我是否正確使用了if,elif語句。沒有得到我想要的輸入

condition=input("What is the condition of the phone(New or Used)?") 
     if(condition != "New") or (condition != "new"): 
      print("Invalid input") 
      return main() 
     elif(condition != "Used") or (condition != "used"): 
      print("Invalid input") 
      return main() 


     gps=input("Does the phone have gps(Yes or No)?") 
     if(gps != "Yes") or (gps != "yes"): 
      print("Invalid input") 
      return main() 
     elif(gps != "No") or (gps != "no"): 
      print("Invalid input") 
      return main() 


     wifi=input("Does the phone have wifi(Yes or No)?") 
     if(wifi != "Yes") or (wifi != "yes"): 
      print("Invalid input") 
      return main() 
     elif(wifi != "No") or (wifi != "no"): 
      print("Invalid input") 
      return main() 


     camera=input("Does the phone have a camera(Yes or No)?") 
     if(camera != "Yes") or (camera != "yes"): 
      print("Invalid input") 
      return main() 
     elif(camera != "No") or (camera != "no"): 
      print("Invalid input") 
      return main() 
+0

您的代碼顯示了在「if語句」中組合多個條件的主題缺乏理解。最好再多做一些Google搜索。 – StillLearnin

+0

對於python 2用「raw_input」代替「input」 –

+0

假設你輸入「Yes」,想想第一次檢查的結果是什麼。這是錯誤的,因此執行下降到「elif」 – StillLearnin

回答

2

你的程序是做什麼它被寫做。每input()行後,它執行以下操作:

print("Invalid input") 
return main() 

例如,把你的第一個代碼塊:

condition=input("What is the condition of the phone(New or Used)?") 
if(condition != "New") or (condition != "new"): 
    print("Invalid input") 
    return main() 
elif(condition != "Used") or (condition != "used"): 
    print("Invalid input") 
    return main() 

說你在提示符下輸入New,所以condition目前擁有價值"New"。在if語句中的第一個測試將產生False - "New" != "New"是錯誤的,因爲實際上"New"確實等於"New"or之後的下一個條件現在已經過測試,並且返回True(因爲"New"實際上並不等於"new"),所以該塊將被執行,打印"Invalid input"並全面運行main()

+0

親愛的Downvoters:我很想用我的回答解決您的擔憂,但您沒有說明任何問題。現在,我很傷心:( – MattDMo

+0

不完全是一個有用的答案爲明顯的問題是缺乏瞭解的條件進行編程。OP已經懷疑這是問題,你的答案沒有幫助所以現在他很傷心:( – StillLearnin

+0

@StillLearnin (條件!=「新建」)或(條件!=「新建」)#現在我已經對自己的答案感到滿意了,希望其他人和我們的操作系統也會一樣。 – MattDMo

1
if (condition != "New") or (condition != "new"): 

這將始終爲真,因爲condition不能同時爲「新」和「新」。至少有一個比較結果爲True,使整個表達式等於True。這可以通過將or切換到and來解決。代碼中的其他條件也相同。

即使您更改支票

(condition != "New") and (condition != "new") 

你還有另外一個問題。如果condition == "New",則檢查是False,評估跳轉到elif

elif (condition != "Used") and (condition != "used"): 

elif一定是假的,因爲condition不是 「拿來主義」,也不是 「拿來主義」。您可以通過將所有的測試中解決這個相同的,如果:

if (condition != "New") and (condition != "new") and (condition != "Used") and (condition != "used"): 

然而,更地道的方式來做到這一點是:

if condition not in ["New", "new", "Used", "used"]: 
    print("Invalid input") 
    return main() 

這將打印「無效的輸入」,如果condition不是["New", "new", "Used", "used"]中的字符串之一。

更好的是完全忽略套管。要做到這一點,只需撥打.lower()input

condition = input("What is the condition of the phone(New or Used)?").lower() 

就可以檢查

if condition not in ["new", "used"]: 

,你的代碼會接受輸入,如「新」,「拿來主義」,「新的」和失敗( print("Invalid input"))用於其他輸入。

+0

非常感謝你!對不起張貼這是一個馬虎的問題,這是我的第一篇文章,我很欣賞這個解釋和努力,謝謝!!!!! – SavageSammy