• Register
0 votes
1.5k views

Problem :

I am facing following type error while trying to execute the Python program
deprecationwarning: object of type <class 'float'> cannot be safely interpreted as an integer.
6.9k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

Please note the range() can only work with integers but the dividing with  / operator will always results in a float value:

e.g.
>>> 450 / 10
45.0
>>> range(450 / 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

So please make the value an integer again by following method:

for i in range(int(c / 10)):

or you can use the // floor division operator:

for i in range(c // 10):
38.6k points
0 votes

Solution:

In:

for i in range(c/10):

You're creating a float as a result - to fix this use the int division operator:

for i in range(c // 10):

range() can only work with integers, but dividing with the / operator always results in a float value:

>>> 450 / 10
45.0
>>> range(450 / 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer

Make the value an integer again:

for i in range(int(c / 10)):

or use the // floor division operator:

for i in range(c // 10):

As shown below, range only supports integers:

>>> range(15.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got float.
>>> range(15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>>

However, c/10 is a float because / always returns a float.

Before you put it in range, you need to make c/10 an integer. This can be done by putting it in int:

range(int(c/10))

or by using //, which returns an integer:

range(c//10)

 

31.7k points

Related questions

0 votes
1 answer 18 views
18 views
Problem: Why this message keep shown to me :deprecationwarning: object of type <class 'float'> cannot be safely interpreted as an integer.
asked Mar 15 Wafa Abu Yousef 6.1k points
0 votes
1 answer 11 views
11 views
Problem: List object cannot be interpreted as an integer
asked Jul 2 asha 121k points
0 votes
1 answer 29 views
29 views
typeerror: 'float' object cannot be interpreted as an integer
asked Feb 16 ArifulIslam 9.9k points
0 votes
1 answer 102 views
102 views
Problem: TypeError: &lsquo;float&rsquo; 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.How to solve this error
asked Jan 6 Mashhoodch 13k points
1 vote
1 answer 132 views
132 views
Problem: Here is my sample code below from turtle import * speed(0) hideturtle() c = 450 def grid(x,y,a): seth(0) pu() goto(x,y) pd() for i in range(4): forward(a) rt(90) for i in range(c/10): seth(0) forward(10) rt(90) forward(c) backward(c ... \Desktop\gridv2.py", line 15, in grid for i in range(c/10): TypeError: 'float' object cannot be interpreted as an integer What&rsquo;s wrong with my code?
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 14 views
14 views
Problem: Float' object cannot be interpreted as an index.
asked Jul 4 Shima 126k points
0 votes
1 answer 5 views
5 views
Problem: List' object cannot be interpreted as an integer.
asked Jun 30 Shima 126k points
0 votes
1 answer 6 views
6 views
Problem: Is there any way around this : Numpy.float64' object cannot be interpreted as an integer?
asked Jun 23 Mezbah 130k points
0 votes
1 answer 83 views
83 views
Problem: What is wrong here : Tuple' object cannot be interpreted as an integer?
asked May 22 Sawyer Parviz 168k points
0 votes
1 answer 214 views
214 views
Problem: I'm trying to find an answer to this complication >Typeerror: 'tuple' object cannot be interpreted as an integer.
asked May 7 Danyal Sjoerd 19.2k points