2014-08-29 77 views
0

如何包含while循环以保持用户猜测,直到他们在以下Jython程序中获得正确答案为止?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

试试这个:

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

它没有做任何事情,并没有表现出任何的错误。 – 2014-08-29 09:12:32

+0

我编辑了我的回答,发生的原因是因为所有内容都应该在'while'循环中。 – 2014-08-29 09:52:24

+0

=======加载Progam ======= 尽量猜数:-3 尽量猜数: – 2014-08-29 12:18:48