python猜数游戏(练习while无限循环)

number的值需要预先设置好,同样,还有条件变量

number = 23
running = True

while running:
    guess = int(input('Enter an integer : '))

    if guess == number:
        print('Congratulations, you guessed it.')
        # 这将导致 while 循环中止
        running = False
    elif guess < number:
        print('No, it is a little higher than that.')
    else:
        print('No, it is a little lower than that.')
else:
    print('The while loop is over.')
    # 在这里你可以做你想做的任何事

print('Done')

运行结果(示例):
在这里插入图片描述

还有一个字符输入的循环:

while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    if len(s) < 3:
        print('Too small')
        continue
    print('Input is of sufficient length')
    # 自此处起继续进行其它任何处理

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CSDN_YJX/article/details/120859042