分数只考虑最后的ans

2024-09-30 08:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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,我如何正确地跟踪分数


Tags: importisrandom分数wordscorewordsjack
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:37

When I print the score its always either -1 or 1[...]

发生这种情况的原因是score在每次迭代开始时在循环内重新初始化

像这样把它移到外面:

score = 0
while i < 5:
    ...

相关问题 更多 >

    热门问题