0
for i in range(1.5,21):
    print(i)

Whenever i run this code i get an error:

TypeError: 'float' object cannot be interpreted as an integer

Is there any built in modules for getting a range of floats?

richardev
  • 976
  • 1
  • 10
  • 33
SCAR lESS
  • 11
  • 1
  • What would you want range() to return for floats? There are infinite floats in between any 2 numbers (that arent the same number) – CircuitSacul May 24 '20 at 22:55
  • You can to range with integers (no possible with floats) and, inside loop, obtain the floats you want from the corresponding integer. – jlbmdm May 24 '20 at 23:01
  • 3
    Does this answer your question? [range() for floats](https://stackoverflow.com/questions/7267226/range-for-floats) – ggorlen May 24 '20 at 23:07

3 Answers3

2

Assuming you know how many values you'd like to divide the range 1.5, 21 into, numpy.linspace will do the job:

import numpy

for i in numpy.linspace(1.5,21,num=10):
    print(i)

output:

$ python main.py
1.5
3.6666666666666665
5.833333333333333
8.0
10.166666666666666
12.333333333333332
14.5
16.666666666666664
18.833333333333332
21.0

Or if you'd rather go by step than number of output values, you can use numpy.arange:

import numpy

for i in numpy.arange(1.5,21,step=0.5):
    print(i)

output:

$ python main.py
1.5
2.0
2.5
3.0
[-- cut --]
19.0
19.5
20.0
20.5
Samiser
  • 71
  • 5
0

You cannot get a range of floats. Think about it. Can a computer do one and a half (1.5) of a task? If you simply want to give it a float and round it up (or down) then use round() function, this will round the float to an int

John
  • 197
  • 1
  • 3
  • 14
0

I guess this is an optimal solution for you but be aware that this code can return sometimes bad results ! as duplicating elements or even missing some !!!

def frange(*args):
    
    ((res:=[],gdigits:=lambda n,r:len(str(n)[::-1].split('.')[0])if'.'in str(n)else r,chf:=lambda n:True if type(n)==float else False),
    ((len(args)==0)and(
        a:=0.,
        b:=10.,
        c:=1.)),        
    ((len(args)==1)and((chf(args[0])and(
        a:=0.,
        b:=args[0]*1.,
        c:=1/10**gdigits(args[0],0))
        )or(
        a:=0,
        b:=args[0],
        c:=10**gdigits(args[0],0)))),       
    ((len(args)==2)and((any(map(lambda x: True if int(x)!=x else False,args))and(
        a:=args[0]*1.,
        b:=args[1]*1.,
        c:=1/10**max(gdigits(a,len(str(a))),gdigits(b,len(str(b)))))
        )or(
        a:=args[0],
        b:=args[1],
        c:=1))),
    ((len(args)==3)and((any(map(lambda x: True if int(x)!=x else False,args))and(
        a:=args[0]*1.,
        b:=args[1]*1.,
        c:=args[2]*1. if args[2]!=0 else 1)
        )or(
        a:=args[0],
        b:=args[1],
        c:=args[2] if args[2]!=0 else 1)))
        )   
    res.append(a)
    (a<b and [res.append(a:=round(a+c,len(str(c)[::-1].split('.'))if'.'in str(c)else 0)) for _ in iter(lambda:a<b,0)])
    (b<a and [res.append(a:=round(a-c,len(str(c)[::-1].split('.'))if'.'in str(c)else 0)) for _ in iter(lambda:b<a,0)])      
    return (i for i in res)

print('****    Test Cases    ****\n')
print(f'no args: {list(frange())}\n')
print(f'float arg: {list(frange(0.58))}\n')
print(f'int arg: {list(frange(10))}\n')
print(f'2  float args: {list(frange(1.11,1.3))}\n')
print(f'reversed 2 float args: {list(frange(1.3,1.11))}\n')
print(f'2 int args: {list(frange(3,8))}\n')
print(f'reversed 2 int args: {list(frange(8,3))}\n')
print(f'3 float args: {list(frange(1.75,3.4,0.05))}\n')
print(f'reversed 3 float args: {list(frange(3.4,1.75,0.05))}\n')
print(f'3 int args: {list(frange(2,11,3))}\n')
print(f'reversed 3 int args: {list(frange(11,2,3))}\n')
print(f'mixed 1: {list(frange(3,12.4,0.2))}\n')
print(f'mixet 2: {list(frange(12,1,1.2))}\n')
Ervis Meta
  • 13
  • 1