Home > other >  I'm not sure why I'm getting a TypeError (float object cannot be interpreted as integer) i
I'm not sure why I'm getting a TypeError (float object cannot be interpreted as integer) i

Time:03-12

Following the Think Python textbook (second edition).

Trying to get past this one section but repeatedly running into an error that seems very confusing to me, as I've simply copied the code from the book.

import turtle
def polygon(t, length, n):
    for i in range(n):
    t.fd(length)
    t.lt(360/n)
import math
def circle(t, r):
    circumference = 2 * math.pi * r
    n = int(circumference/3)   3
    length = circumference/n
    polygon(t, n, length)

bob=turtle.Turtle()
circle(bob, 100)

When I do this I end getting an error code:

Traceback (most recent call last):

.... circle(bob, 100)

... polygon(t, n, length)

... for i in range(n):

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

I've seen other similar questions on stackoverflow about this section but I haven't seen anyone with a Type Error before. I think this section of the book must be written in a kind of confusing way since people seem to be making a lot of errors here, and I've definitely struggled with trying to follow what code goes with what code and what code explicitly does not.

This is the book, by the way

CodePudding user response:

You've swapped the parameters n and length in the call to polygon. So inside of polygon, you call range() on a float. That leads to the type error.

  • Related