小编典典

循环播放,直到特定用户输入

python

我正在尝试编写一个数字猜测程序,如下所示:

def oracle():
    n = ' '
    print 'Start number = 50'
    guess = 50 #Sets 50 as a starting number
    n = raw_input("\n\nTrue, False or Correct?: ")
    while True:
        if n == 'True':
            guess = guess + int(guess/5)
            print
            print 'What about',guess, '?'
            break
        elif n == 'False':
            guess = guess - int(guess/5)
            print
            print 'What about',guess, '?'
            break
        elif n == 'Correct':
            print 'Success!, your number is approximately equal to:', guess

oracle()

我现在想做的是让if / elif /
else命令序列循环执行,直到用户输入“正确”为止,即程序声明的数字大约等于用户数,但是如果我不知道用户数量,我无法想到如何实现和if语句,并且尝试使用“
while”也无效。


阅读 175

收藏
2020-12-20

共1个答案

小编典典

作为@Mark Byers方法的替代方法,可以使用while True

guess = 50     # this should be outside the loop, I think
while True:    # infinite loop
    n = raw_input("\n\nTrue, False or Correct?: ")
    if n == "Correct":
        break  # stops the loop
    elif n == "True":
        # etc.
2020-12-20