而循环在Jython随机模块中

时间:2014-08-29 06:22:54

标签: jython jython-2.5

我怎样才能包含一个while循环来让用户猜测,直到他们在下面的Jython程序中得到正确的答案?

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")

1 个答案:

答案 0 :(得分:1)

试试这个:

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")