使用范围时,"TypeError:'Float&39;对象不能解释为整数&q;是什么意思?

2022-03-12 00:00:00 python typeerror

问题描述

我不明白为什么不能使用我的变量c

编码:

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)

我收到以下错误消息:

Traceback (most recent call last):
  File "C:Users
ickDesktopgridv2.py", line 35, in <module>
    grid(0,0,c)
  File "C:Users
ickDesktopgridv2.py", line 15, in grid
    for i in range(c/10):
TypeError: 'float' object cannot be interpreted as an integer

解决方案

位于:

for i in range(c/10):

您将作为结果创建一个浮点数-要解决此问题,请使用INT除法运算符:

for i in range(c // 10):

相关文章