我不确定为什么我会在这段代码中收到 TypeError(浮点对象不能解释为整数):
I'm not sure why I'm getting a TypeError (float object cannot be interpreted as integer) in this code:
遵循 Think Python 教科书(第二版)。
试图通过这一节,但反复 运行 出现一个错误,这让我很困惑,因为我只是从书中复制了代码。
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)
当我这样做时,我得到了一个错误代码:
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
我在 Whosebug 上看到过关于此部分的其他类似问题,但我之前从未见过任何类型错误的人。我认为本书的这一部分必须以一种令人困惑的方式编写,因为人们似乎在这里犯了很多错误,而且我确实在努力遵循哪些代码与哪些代码相符以及哪些代码明确不相符.
您在 polygon
的调用中交换了参数 n
和 length
。所以在 polygon
中,你调用了一个浮点数 range()
。这会导致类型错误。
遵循 Think Python 教科书(第二版)。
试图通过这一节,但反复 运行 出现一个错误,这让我很困惑,因为我只是从书中复制了代码。
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)
当我这样做时,我得到了一个错误代码:
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
我在 Whosebug 上看到过关于此部分的其他类似问题,但我之前从未见过任何类型错误的人。我认为本书的这一部分必须以一种令人困惑的方式编写,因为人们似乎在这里犯了很多错误,而且我确实在努力遵循哪些代码与哪些代码相符以及哪些代码明确不相符.
您在 polygon
的调用中交换了参数 n
和 length
。所以在 polygon
中,你调用了一个浮点数 range()
。这会导致类型错误。