How to use a decimal range() step value?

前端 未结 30 2648
醉话见心
醉话见心 2020-11-21 22:34

Is there a way to step between 0 and 1 by 0.1?

I thought I could do it like the following, but it failed:

for i in range(0, 1, 0.1):
    print i
         


        
30条回答
  •  滥情空心
    2020-11-21 23:10

    Increase the magnitude of i for the loop and then reduce it when you need it.

    for i * 100 in range(0, 100, 10):
        print i / 100.0
    

    EDIT: I honestly cannot remember why I thought that would work syntactically

    for i in range(0, 11, 1):
        print i / 10.0
    

    That should have the desired output.

提交回复
热议问题