2017-09-05 11 views
-3

我正在嘗試完成一個已被設置爲回校工作的程序,但我之前從未使用過Python,所以我正在努力查看錯誤的位置所在。任何幫助將不勝感激,最好週四,因爲這是工作到期!Python名稱錯誤:未定義全局名稱'score'

#revision platform 
def Spelling(): 
    global test 
    global score 
    score = 0 
print("Welcome to the Spelling test.") 
Q1=raw_input("Which spelling is correct:\nChangeable\nChangeble\n") 
if Q1=="Changeable": 
    print("Correct") 
    score += 1 
else: 
    print("Incorrect") 
Q2=raw_input("Which spelling is correct:\nThreshhold\nThreshold\n") 
if Q2=="Threshold": 
    print("Correct") 
    score += 1 
else: 
    print("Incorrect") 
Q3=raw_input("Which spelling is correct:\nScent\nSent\nCent\n") 
if Q3=="Scent" or Q3=="Sent" or Q3=="Cent": 
    print("Trick Question they were all right!") 
    score += 1 
    print("Your score is:" ,score) 
else: 
    print("Incorrect") 
    print("Your score is:",score) 

謝謝你的幫忙!

+2

它看起來像你的縮進關閉。 –

+0

你會得到什麼錯誤?你能更具體地說明問題是什麼嗎?如果您可以添加追蹤以及 –

+0

這將是有幫助的問題是縮進。但爲什麼你將這些變量聲明爲全局? python中的 –

回答

0
#revision platform 
def Spelling(): 
    score = 0 
    print("Welcome to the Spelling test.") 
    Q1=raw_input("Which spelling is correct:\nChangeable\nChangeble\n") 
    if Q1=="Changeable": 
     print("Correct") 
     score += 1 
    else: 
     print("Incorrect") 
    Q2=raw_input("Which spelling is correct:\nThreshhold\nThreshold\n") 
    if Q2=="Threshold": 
     print("Correct") 
     score += 1 
    else: 
     print("Incorrect") 
    Q3=raw_input("Which spelling is correct:\nScent\nSent\nCent\n") 
    if Q3=="Scent" or Q3=="Sent" or Q3=="Cent": 
     print("Trick Question they were all right!") 
     score += 1 
     print("Your score is:" ,score) 
    else: 
     print("Incorrect") 
     print("Your score is:",score) 

只需撥打拼寫()函數,那麼你的程序將工作