Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中是否有大于但小于的函数?_Python_Python 3.x - Fatal编程技术网

python中是否有大于但小于的函数?

python中是否有大于但小于的函数?,python,python-3.x,Python,Python 3.x,我有一段代码,我只是在玩,因为我是python新手,这是: a = 0 while a < 10: a = a + 1 print("A is Less than 10") a=0 而a

我有一段代码,我只是在玩,因为我是python新手,这是:

a = 0
while a < 10:
    a = a + 1
    print("A is Less than 10")
a=0
而a<10:
a=a+1
打印(“A小于10”)
我想再添加一些代码,说明: 如果a大于10但小于20,请打印此:
我试过:

a=0
而a<10:
a=a+1
打印(“A小于10”)
而a<20:
a=a+1
打印(“A大于10,但小于20。”)
但所做的只是打印“A大于10,但小于20”
基本上,python中是否有“小于但大于”函数? 顺便说一下,我正在运行版本3

while 10 < a < 20:
    whatever
或者,如果您只想测试单个数字而不是循环,请使用
if

if 10 < a < 20:
    whatever
如果10

注意边界条件。第一个循环结束时,
a
设置为
10
。(事实上,当您打印最后一条“小于10”的消息时,它已经设置为10。)如果您立即检查它是否大于10,您会发现它不大于10。

在Python中,您甚至可以编写

while 10 < a < 20:
    do_smth()
而10
这很有效,正是我想要的,谢谢!我没有使用for循环,而是将代码从“whilea<10:”改为“whilea”,并使用了
if 10 < a < 20:
    whatever
while 10 < a < 20:
    do_smth()