Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/160.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 当llop进入for循环时,如何编写以下内容?_Python_Python 3.x - Fatal编程技术网

Python 当llop进入for循环时,如何编写以下内容?

Python 当llop进入for循环时,如何编写以下内容?,python,python-3.x,Python,Python 3.x,该计划是: Y=100 while y>0: print(y) y-=3 我的老师说它可以用for循环来写,但我不知道怎么写。我想用for循环完成同样的任务 非常感谢您的帮助。正如John Gordon的评论所说,您可以使用range()函数,其中包含三个参数:start、stop和step。但是当我们想从一个较大的数字变为一个较小的数字时,我们必须定义步骤(这是一个可选参数) 在这种情况下,您的步骤将是-3。 所以你是这样做的: for y in range(100,

该计划是:

Y=100
while y>0:
    print(y)
    y-=3
我的老师说它可以用for循环来写,但我不知道怎么写。我想用for循环完成同样的任务


非常感谢您的帮助。

正如John Gordon的评论所说,您可以使用
range()
函数,其中包含三个参数:start、stop和step。但是当我们想从一个较大的数字变为一个较小的数字时,我们必须定义步骤(这是一个可选参数)

在这种情况下,您的步骤将是-3。 所以你是这样做的:

for y in range(100, 0, -3):
    print(y)
给你:

100
97
94
91
88
.
.
.

一些额外的帮助:

看看
range()
函数。