Decrementing for loops [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-03 06:11:50

问题


I want to have a for loop like so:

for counter in range(10,0):
       print counter,

and the output should be 10 9 8 7 6 5 4 3 2 1


回答1:


a = " ".join(str(i) for i in range(10, 0, -1))
print (a)



回答2:


Check out the range documentation, you have to define a negative step:

>>> range(10, 0, -1)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]



回答3:


You need to give the range a -1 step

 for i in range(10,0,-1):
    print i



回答4:


for i in range(10,0,-1):
    print i,

The range() function will include the first value and exclude the second.




回答5:


range step should be -1

   for k in range(10,0,-1):
      print k


来源:https://stackoverflow.com/questions/4767401/decrementing-for-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!