Jython随机模块中的While循环



如何在下面的Jython程序中包含while循环来让用户猜测,直到他们得到正确的答案?

import random
def numberGuess():
    printNow("I'm thinking of a number between 1 and 10")
    guess = 0 
    randNum = random.randrange(1,11) 
    guess  = int(input("Try to guess the number: ")) 
    if guess > 10:
        print("Wrong! You guessed too high")
    elif guess < 1:
        print("Wrong! You guessed too low")
    else :
        print(" you got it")

试试这个:

import random
while True:
    print("I'm thinking of a number between 1 and 10")
    randNum = random.randrange(1, 11)
    guess = int(input("Try to guess the number: "))
    if guess == randNum:
        print("You got it")
        break
    else:
        if guess > 10:
            print("Wrong! You guessed too high")
        elif guess < 1:
            print("Wrong! You guessed too low")

相关内容

  • 没有找到相关文章

最新更新