【问题标题】:While loop in Jython random moduleJython 随机模块中的 While 循环
【发布时间】:2014-08-29 06:22:53
【问题描述】:

如何包含一个 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")

【问题讨论】:

    标签: jython jython-2.5


    【解决方案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")
    

    【讨论】:

    • 它什么也没做,也没有显示任何错误。
    • 我已经编辑了我的答案,发生的原因是因为一切都应该在 while 循环内。
    • ======= 正在加载程序 ======= 尝试猜数字:-3 尝试猜数字:
    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 2020-12-15
    • 2018-03-20
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多