Python range function

后端 未结 6 954
广开言路
广开言路 2021-02-07 05:53

Say I want to loop from 0 to 100 but with a step of 1/2. If you try

for i in range(0, 100, 0.5):
    whatever
         


        
6条回答
  •  遇见更好的自我
    2021-02-07 06:25

    You have to use integer steps for range() and xrange(). That's why your 0.5 step gets internally converted to 0 and you get that error. Try for i in [j / 2.0 for j in xrange(100 * 2)]:

提交回复
热议问题