random_words = ['michael ', 'is', 'cool', 'avyn','tooburt','mooburt','myself','jack','rahim']
import random
i = 0
while i < 5:
score = 0
random_word = random.choice(random_words)
word = input(random_word + ' ')
if random_word == word:
score += 1
else:
score -= 1
i += 1
print('you scored ' + str(score))
当我打印得分时,它始终为-1或1,如何正确跟踪得分
答案 0 :(得分:2)
当我打印得分时,它总是-1或1 [...]
这是因为score
在每次迭代开始时在循环内重新初始化。
将它移到外面:
score = 0
while i < 5:
...