我在 Python 代码中的计数器无法正常工作

问题描述 投票:0回答:0

我是一个新手程序员。我正在写一个程序。它的本质如下:向用户显示一个随机数。如果数字是偶数,他需要回答“是”,如果是奇数,他需要回答“否”。用户必须连续给出三个问题的正确答案。但是我的计数器不工作,我不明白为什么......

我的密码是:

来自随机进口 randint

分数 = 3

def is_even_game():

print('Welcome!')
print('May I have your name?')
name = input()
print(f'Hello, {name}!')

rand_num = randint(1, 100)

print(f'Question: {rand_num}')
text = input()
print('Your answer: ' + text)

counter = 0
while counter <= score:
    if rand_num % 2 == 0:
        answer = 'yes'
    else:
        answer = 'no'

    if text != answer:
        print(f'"{text}" is wrong answer. Correct answer was "{answer}"')
        print(f"Let's try again, {name}!")
        break
    print('Correct!')
    counter += 1
else:
    print(f'Congratulations, {name}!')

is_even_game()

python-3.x counter
© www.soinside.com 2019 - 2024. All rights reserved.