. Advertisement .
..3..
. Advertisement .
..4..
I am trying to write a python that returns the invalid answer. I don’t know where the incorrect command is in the “typeerror float object cannot be interpreted as an integer”. My detail is:
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)
and I end up with the warning 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
Pls, suggest the best answer to fix it.
The cause:
In:
The
/
is being used and as a result, you’re creating an effervescent float but not an integer.Solution:
To fix this, use the int division operator.
range()
is only compatible with integers. However,/
can be used to divide integers.Remake the value into an integer:
Or, you can use the
//
floor operator: