首页 > 解决方案 > 如何循环这个问题直到它的答案是正确的

问题描述

如果他们在回答“你想再玩一次”时说“不”,我该如何让它再次循环,这样如果他们说“不”,它会继续循环回到那个问题,直到他们说“是”?

import random

def main(): 
    n = random.randint(3,10)
    guess = int(input(" So "+ name +" guess a number from 3 to 10: "))
    while n != "guess":
        print
        
        if guess < n:
            print("Sorry "+ name +" but, that guess is low")
            guess = int(input("Guess a number from 3 to 10: "))
            
        elif guess > n:
            print("Come on "+ name +" that guess is high")
            guess = int(input("Guess a number from 3 to 10: "))
            
        else:
            print("Congratulations "+ name+" you guessed it!")
            break
    
    restart = input("Do you want to play again: ").lower()
    if restart==("yes"):
        main()

    elif restart ==("no"):
       print("Are you sure about that?")
       restart = input("Do you want to play again: ").lower()
       main()

标签: python

解决方案


尝试这个:

while True:
    restart = input("Do you want to play again: ").lower()
    if restart == "yes":
        break
    elif restart == "no":
        print("Are you sure about that?")
main()

推荐阅读