Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm not sure why I'm getting a TypeError (float object cannot be interpreted as integer) in this code:

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading