2014-08-29 81 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