python for循环获得数字的阶乘?
# factorial program
n = int(input())
fact = 1
for i in range(n,1,-1): #line4 # this line print the range in reverce
fact = fact*i
print(fact)
有人解释了这一行4。循环如何反向打印n?
# factorial program
n = int(input())
fact = 1
for i in range(n,1,-1): #line4 # this line print the range in reverce
fact = fact*i
print(fact)
some explain the line4. how is the for loop printing the n in reverse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
range()
函数的3个主要参数开始,停止和步骤。如果您从n
开始停止1,并每次执行循环时将索引变量降低1个单位,则将以相反顺序循环循环。例如:
此循环将从
i = 10
开始,正如您可以从步骤值中看到的那样,它将其值降低1,直到达到停止值(excrusive) ,在这种情况下,也有1个。因此,输出将是:The 3 main parameters of the
range()
function are start, stop and step. If you start atn
to stop on 1 and decrease the index variable by 1 unit every time the loop executes, it will loop the values in reverse order.For example:
This loop will start at
i=10
, as you can see from the step value, it will decrease its value by 1 until reach the stop value (exclusive), in this case, 1 too. So the output will be: