for i in range(start,stop,step) Python 3

后端 未结 2 1912
星月不相逢
星月不相逢 2021-01-20 19:09

I want to list a range of numbers and I\'m using the \"for i in range(start,stop,step)\".

def range():
    P=36
    for i in range(P+1,0,-1):
        P=P-4
          


        
2条回答
  •  攒了一身酷
    2021-01-20 19:44

    What's happening is that using range, creates you a list from 36 to 0 at the beggining and does a for loop 37 times. Your var P has no longer effect on the number of loops if you change it in your loop.

    In your first case you'll have 36-37*4 = -112

    In your second case you have your loop executed 9 times thats why you get 0 36 -9*4 = 0 :)

    Try using xrange it should create a value for each loop and i think it will stop at 0.

提交回复
热议问题