2013-01-17 28 views
0

我想在這個遊戲中保持一個分數,所以我設置了一個分數變量,並且每當答案被正確回答時,它就增加1分來得分,如果你得到一個不正確的答案,它會扣除一個分數。當我在結束打印得分,它仍然是等於0評分在這裏不起作用? - Python

score = 0 
q1answer = ("metallica", "slayer", "megadeth", "anthrax") 

answerinput = str(input("name one of the 'Big Four' metal bands'")) 

if answerinput.lower() in q1answer: 
    print ("You got the right answer!") 
    score + 1 

else: 
    print ("That is the wrong answer...") 
    score - 1 
print (score) 

回答

1

score + 1score - 1只是表達式;他們實際上沒有做任何事情。要實際更改score,請使用score += 1score -= 1

(另外,使用一組!大括號!正如前面提到的;)

2

score + 1只是一種表達,不改變score變量的實際值。這與0 + 1基本相同,因爲python只會得到score的值,並將1添加到它收到的值,而不是變量本身。

要解決此問題,您需要重新指定score以匹配其當前值加上一個:score = score + 1或更簡單的版本:score += 1。並去除得分,只需使用減號:score = score - 1或更容易score -= 1

相關問題