2014-10-11 82 views
-1

我是一個剛剛開始編碼4-5周前的新手編碼器。我所取得的最好成績是一個「絕密」網站的基本Python用戶名和密碼登錄頁面(網站是假的)。然而,爲了讓我更加了解基本編碼(我最近一直在做一些不相關的事情),我試着製作一個基本的兒童遊戲來處理字母表。這是我現在的代碼:爲什麼這些參數不適用於Python 3?

name = input("What's Your Name?: ") 
print("Welcome" , name , "to the 3 lucky guess alphabet skills builder!") 
print("Let's get started:") 
C = input("What Is The 3rd Letter of The Alphabet: ") 
if C == 'C' or 'c': 
    print("Congraulations!") 
else: 
    print("Think We Should Retry That!") 
    C 
    if C == 'C' or 'c': 
      print("That's Better!") 
Z = input("What Is The Last Letter of The Alphabet: ") 
if Z == 'Z' or 'z': 
    print("You're Really Good At This! One More!") 
else: 
    print("Have Another Go!") 
    Z 
    if Z == 'Z' or 'z': 
     print("That's More Like It! Last One!") 
J = input("What Is The 10th Letter Of The Alphabet: ") 
if J == 'J' or 'j': 
    print("Great! How Many Did You Get In Total?") 
else: 
    print("Unlucky, Better Luck Next Time!") 

total = input("How Many Did You Get In Total?: " , print("Out Of 3!") 
print("Wow! You Got" , total , "! Well Done" , name , "!!!") 
exit 

爲什麼沒有任何'其他'的參數工作?

此外,爲什麼不能倒數第二的代碼工作 - 它只是說明語法錯誤!

我試着縮進所有其他語句,但也導致語法錯誤!

請幫忙! :)

+0

另請參閱http://stackoverflow.com/q/15112125/3001761 – jonrsharpe 2014-10-11 20:26:00

回答

2

的,如果你寫的,像下面

if C == 'C' or 'c': 

語句不你是什麼意思。 or之後的表達式僅檢查'c'是否爲真,它總是這樣。這就是爲什麼else:之後的代碼不能執行。

你必須把它寫這樣的:

if C == 'C' or C == 'c': 
+0

這可能是您現在看到的錯誤,但是當您通過該錯誤時,您會在我的答案中發現錯誤。既然你不能同時接受,我建議你接受這個,因爲它更接近你想問的問題。 :) – 2014-10-11 20:29:56

1

這是很難知道你所說的「不工作」的意思 - 你應該更具體的瞭解您所看到的錯誤。你是指這個嗎?

else: 
    print("Have Another Go!") 
    Z 
    if Z == 'Z' or 'z': 
     print("That's More Like It! Last One!") 

身體的第二行簡單地評估變量Z - 它不會改變任何東西。因此,條件跟隨它仍會返回與上次評估相同的結果。

而且,對方的回答指出,

if a = "foo" or "bar" 

永遠是真實的,因爲「酒吧」是一個非假值,和或與任何非假值爲True。

相關問題