How to use a decimal range() step value?

前端 未结 30 2134
醉话见心
醉话见心 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:12

    The range() built-in function returns a sequence of integer values, I'm afraid, so you can't use it to do a decimal step.

    I'd say just use a while loop:

    i = 0.0
    while i <= 1.0:
        print i
        i += 0.1
    

    If you're curious, Python is converting your 0.1 to 0, which is why it's telling you the argument can't be zero.

提交回复
热议问题