Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "TypeError: 'float' object cannot be interpreted as an integer" mean when using range?

I don't understand why I can't use my variable c.

code:

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)     for i in range(c/10):         seth(0)         rt(90)         forward(10)         rt(90)         forward(c)         backward(c)     pu()     goto(a+10,0)     write("x")     goto(0,a+10)     write("y")     pd()  grid(0,0,c) grid(-c,0,c) grid(-c,c,c) grid(0,c,c) 

I get the following error message:

Traceback (most recent call last):   File "C:\Users\nick\Desktop\gridv2.py", line 35, in <module>     grid(0,0,c)   File "C:\Users\nick\Desktop\gridv2.py", line 15, in grid     for i in range(c/10): TypeError: 'float' object cannot be interpreted as an integer 
like image 385
remorath Avatar asked Nov 06 '13 23:11

remorath


People also ask

What does float object Cannot be interpreted as an integer mean?

The Python "TypeError: 'float' object cannot be interpreted as an integer" occurs when we pass a float to a function that expects an integer argument. To solve the error, use the floor division operator, e.g. for i in range(my_num // 5): .

How do I fix TypeError float object is not iterable?

The Python "TypeError: 'float' object is not iterable" occurs when we try to iterate over a float or pass a float to a built-in function like, list() or tuple() . To solve the error, use the range() built-in function to iterate over a range, e.g. for i in range(int(3.0)): .

What does list object Cannot be interpreted as an integer mean?

The Python "TypeError: 'list' object cannot be interpreted as an integer" occurs when we pass a list to a function that expects an integer argument, e.g. range() . To solve the error, either pass the length of the list, e.g. len(my_list) or pass an integer to the function. Here is an example of how the error occurs.


1 Answers

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): 
like image 138
Jon Clements Avatar answered Sep 30 '22 18:09

Jon Clements