2016-07-06 44 views
0

變量c將不會在下面的代碼運行:的Python:在嵌套的變量,如果其他人將無法運行

A = raw_input("How are you today") 
if A == "Good" or "Fine" or "good" or "fine" or "great" or "Great" or "Wonderful" or "wonderful": 
    print("I am glad you are having a good day") 
    B = raw_input("What made your day good") 
    if B == "everything" or "Everthing": 
     print("Everything! You must be having a very good day") 
    else: 
     print("It sounds like you had a very intersting day") 

     C = raw_input("Do you want to hear about my day? [y/n]") 
     if C == "y" or "Y": 
      print("I sat around as an unused computer!") 
     else: 
      print("I guess I am just an annoying computer") 
else: 
    print("I am sorry you are having a bad day") 
+2

可能的重複[如何測試一個變量對多個值?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) –

+1

請爲您的下一個StackOverflow問題正確格式化您的代碼。用不正確的縮進來調試Python是不可能的。在這種情況下,很容易看到發生了什麼,因爲你犯了一個非常常見的錯誤。請參閱相關問題以獲得答案。 –

+0

最後的'else'語句沒有對應的'if'。 –

回答

-1

你必須每次在if語句比較。 假設你正在試圖做到這一點:

>>> b = "bhansa" 
>>> if b== "A" or "B": 
    print "yes" 


yes 

這是不正確的。

A = raw_input("How are you today") 
if A == "Good" or A=="Fine" or A=="good" or A=="fine" or A=="great" or A=="Great" or A=="Wonderful" or A=="wonderful": 
    print("I am glad you are having a good day") 
    B = raw_input("What made your day good") 
    if B == "everything" or B=="Everthing": 
    print("Everything! You must be having a very good day") 
    else: 
    print("It sounds like you had a very intersting day") 
    C = raw_input("Do you want to hear about my day? [y/n]") 
    if C == "y" or C=="Y": 
     print("I sat around as an unused computer!") 
    else: 
     print("I guess I am just an annoying computer") 
else: 
    print("I am sorry you are having a bad day") 
0

你正在爲你說的它,而它也應該會常常A == X或A == Y和等

另一種方式讓你少打字是運營商使用的比較名單。

這樣

A = "Wonderful" 
if A in ["Good","Fine","good","fine","great","Great","Wonderful","wonderful"]: 
    print "yes" 
else: 
    print "no" 

,這將使你少打字,並有重複A == X爲每一種情況下

的相同的結果,而不是你所要做的,對於所有的變量A,B和C