无法在Python中重新启动While循环

时间:2019-11-10 10:35:23

标签: python-3.x

所以程序没有按我希望的那样运行。如果用户键入1或2,则重复输出  “ nvm” “想再试一次” 无论我输入什么。请帮忙!!!

import random
guess = input("Whats your guess")
comp = random.randrange(0, 10)
print(comp)
guess_correct = True
while guess_correct == True:
    if guess == comp:
        print("Bullseye!!!!!")
    else:
        print("nvm")

    ask = int(input("Wanna try again?If yes type 1, if no type 2"))
    if ask == 1:
        guess_correct = True
    elif ask == 2:
        guess_correct = False

1 个答案:

答案 0 :(得分:0)

使用猜测输入在循环内,我们可以简单地编写:

import random
comp = random.randrange(0, 10)
print(comp)
guess_correct = True
while guess_correct:
    guess = int(input("Whats your guess ? "))
    if guess == comp:
        print("Bullseye!!!!!")
        guess_correct = False
    else:
        print("nvm")
        ask = int(input("Wanna try again?If yes type 1, if no type 2 "))
        if ask == 2:
            guess_correct = False

也不要忘记将输入强制转换为 int ,如果答案正确,那就没有必要要求其他尝试了...