0

我试图在这个游戏中保持分数,所以我设置了一个分数变量,每次正确回答答案时,它会增加 + 1 分,如果你得到不正确的答案,它会扣除一分。当我最后打印分数时,它仍然等于 0。

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)
4

2 回答 2

2

score + 1只是一个表达式,不会改变score变量的实际值。这与说的基本相同0 + 1,因为python只会获取score并添加1到它收到的值,而不是变量本身。

要解决此问题,您需要重新分配score以匹配它的当前值加一:score = score + 1或更简单的版本:score += 1。而要删除分数,只需使用减号:score = score - 1或更简单score -= 1

于 2013-01-17T05:52:11.540 回答
1

score + 1并且score - 1只是表达式;他们实际上什么都不做。要实际更改score,请使用score += 1and score -= 1

(另外,使用一套!花括号!如前所述;)

于 2013-01-17T03:14:01.357 回答