TypeError float object cannot be interpreted as an integer

0 votes

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?

Jun 29, 2020 in Python by akhtar
• 38,260 points
16,811 views

2 answers to this question.

+1 vote

Hi@akhtar,

The range function does not work with floats. Only integer values can be specified as the start, stop, and step arguments. But you can use this in a different way. I have attached one example for your reference.

def range_with_floats(start, stop, step):
    while stop > start:
        yield start
        start += step
for i in range_with_floats(0.1, 0.5, 0.1):
    print(i)

I hope this will help you.

answered Jun 29, 2020 by MD
• 95,460 points
0 votes

Python has two data types that represent numbers: floats and integers. These data types have distinct properties.

If you try to use a float with a function that only supports integers, like the range() function, you’ll encounter the “TypeError: ‘float’ object cannot be interpreted as an integer” error.

TypeError: ‘float’ object cannot be interpreted as an integer

Floating-point numbers are values that can contain a decimal point. Integers are whole numbers. It is common in programming for these two data types to be distinct.

In Python programming, some functions like range() can only interpret integer values. This is because they are not trained to automatically convert floating-point values to an integer.

This error is commonly raised when you use range() with a floating-point number to create a list of numbers in a given range.

answered Dec 16, 2020 by Gitika
• 65,770 points

Related Questions In Python

0 votes
1 answer

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

Hi, @There, The “TypeError: ‘float’ object cannot be ...READ MORE

answered Nov 13, 2020 in Python by Gitika
• 65,770 points
48,658 views
0 votes
1 answer
0 votes
1 answer

Error saying "TypeError: descriptor object needs an argument"

The error is pretty straight forward, toy ...READ MORE

answered May 28, 2019 in Python by Alisha
13,536 views
+1 vote
0 answers

TypeError: only integer scalar arrays can be converted to a scalar index

import cv2 import numpy as np import matplotlib.pyplot as ...READ MORE

Dec 18, 2019 in Python by sandeep
• 130 points
7,370 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
3,931 views
0 votes
1 answer

TypeError: list indices must be integers or slices, not str

Hi@akhtar, As we know that the index of ...READ MORE

answered Oct 15, 2020 in Python by MD
• 95,460 points
13,705 views
0 votes
1 answer

ERROR: Could not build wheels for line-profiler which use PEP 517 and cannot be installed directly.

Hi@akhtar, If you are trying to install with pip ...READ MORE

answered Nov 5, 2020 in Python by MD
• 95,460 points
3,589 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP