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?
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?
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
You cannot get a range
of float
s. 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
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')