While 循环用户输入范围 [英] While loop user input in range

查看:23
本文介绍了While 循环用户输入范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我想向用户询问 1 到 100 之间的数字,如果他们在这些数字之间放了一个数字,它将打印(大小:(输入))并打破循环,但是,如果他们输入它会打印一个 1-100 以外的数字(大小:(输入)),然后继续向他们询问一个数字,但我遇到了一些问题.

I have some code that I want to ask the user for a number between 1-100 and if they put a number between these it will print (Size: (input)) and break the loop, if however, they put in a number outside 1-100 it will print (Size: (input)), and proceed to re-ask them for a number, I have run into some problems though.

c=100
while c<100:
    c=input("Size: ")
    if c == 1 or 2 or 3:
        print ("Size: "+c)
        break
    else:
        print ("Size: "+c)
        print ("Invalid input. Try again.")

推荐答案

应该这样做.

c=input("Size: ")
while int(c)>100 or int(c)<1: 
    #Will repeat until the number is between 1 and 100, inclusively.
    #It will skip the loop if the value is between 1 and 100.

    print ("Size: "+c)
    print ("Invalid input. Try again.")
    c=input("Size: ")

#once the loop is completed, the code following the loop will run
print ("Size: "+c)

这篇关于While 循环用户输入范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆