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

Python

Hi Guys,

I am trying to use the range function in my code. But it is showing me the below error.

TypeError Traceback (most recent call last)
<ipython-input-13-a83306d87fcd> in <module>
1 # floats with python range
----> 2 for i in range(0.1, 0.5, 0.1):
3 print(i)
TypeError: 'float' object cannot be interpreted as an integer

How can I solve this error?
 

1
Answers

Replies

You will need to know that range() works with the integers but not with the floats, you can build their own range generator.



def frange(start, stop, step=1):


    i = start


    while i < stop:


        yield i


        i += step



for i in frange(3.3, 5) will give you the desired result.

 
 

If you want to unleash your potential in this competitive field, please visit the Python course page for more information, where you can find the Python tutorials and Python frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.