• Register
0 votes
587 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

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):
36.1k 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)

 

17.7k points

Related questions

2 votes
1 answer 27 views
27 views
Problem: I am a python newbee. I was trying some code : x = int(input("Input an Integer´╝Ü")) res = [x] while x != 1: ---> for i in range(2, a): if x % i == 0: res.append(i) x = x / i break print(res) but this code shows some error ... did not compile. The error message is : TypeError: 'float' object cannot be interpreted as an integer Can any one tell me why am I getting this kind of error message?
asked Mar 23 LizzyM 6.1k points
1 vote
1 answer 53 views
53 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 Gavin 15.2k points
2 votes
1 answer 314 views
314 views
Problem: I am trying a run a program with a simple list object in my code. But it throwing the same error again and again. The error is: list object cannot be interpreted as an integer python. I am attaching my code snippet below: def userNum( ... winsound.PlaySound("SystemExit", winsound.SND_ALIAS) What could be the possible solution for this program? If possible figure the error, please. Thanks
asked Mar 26 Gavin 15.2k points
1 vote
2 answers 1.1K views
1.1K views
Problem: Hello, I am not extremely versed in numpy, so I am not exactly sure what I am doing wrong. I got it to work with made-up data but I am not sure as to what it is not working with the data I put in. All I am trying to do is fit the data ... plt.ylabel('T') plt.legend() plt.show() The error I am getting is following below TypeError: 'numpy.float64' object cannot be interpreted as an integer
asked Mar 9 Gavin 15.2k points
1 vote
1 answer 80 views
80 views
Problem: For the following code import re import string import pandas as pd import openpyxl import glob import csv import os import xlsxwriter import numpy as np keywords = {"oracles","mining","ico"} df = pd.read_csv(r"C:MYfile.csv", error_bad_lines=False) sep="," , ... j in df.columns] 21 TypeError: argument of type 'float' is not iterable Can anyone sortout the problem and help me to solve this?
asked Apr 26 samhaz 5k points