我如何一遍又一遍地问一个问题,直到用户猜出数字

时间:2019-05-04 13:52:02

标签: python python-3.x

import random

while True:

    number1 = random.randint(1,3)
    guess = float( input( "Guess the number:" ))

    if number1 == guess:
        print("Correct")
        break
    else:
        print("Wrong, try again")
    if guess < number1:
        print ("Your number is too low")
    if guess > number1:
        print ("Your number is too high")


print ("The answer is", number1)

1 个答案:

答案 0 :(得分:0)

尝试一下:

import random
number1 = random.randint(1,3)
guess = 0
while number1 != guess:
    guess = float( input( "Guess the number:" ))

    if guess < number1:
        print ("Wrong! Your number is too low")
    if guess > number1:
        print ("Wrong! Your number is too high")


print ("Correct! The answer is", number1)