为什么第34行返回:TypeError:'int'不支持索引

时间:2019-06-11 14:01:58

标签: python list turtle-graphics

由于下一行出现TypeError,我无法执行此代码

t.goto(a [y]) (顺便说一句,我是初学者,所以请详细说明)

    a = [(285.316954889, 92.7050983125),(242.705098312,176.335575688),(176.335575688, 242.705098312),(92.7050983125,285.316954889),(0.0, 300.0),(-92.7050983125, 285.316954889),(-176.335575688, 242.705098312),(-242.705098312, 176.335575688),(-285.316954889, 92.7050983125),(-300.0, 0.0),(-285.316954889,-92.7050983125),(-242.705098312, -176.335575688),(-176.335575688,-242.705098312),(-92.7050983125, -285.316954889),(0.0, -300.0),(92.7050983125, -285.316954889),(176.335575688, -242.705098312),(242.705098312, -176.335575688),(285.316954889, -92.7050983125),(300.0, 0.0)]

    y = 0

    for x in a:
     for b in range(20):
       t.goto(a[y])
       y = y + 1

2 个答案:

答案 0 :(得分:1)

此错误是因为您为a有2个定义。

a = [(285.316954889, 92.7050983125),(242.705098312,176.335575688),(176.335575688, 242.705098312),(92.7050983125,285.316954889),(0.0, 300.0),(-92.7050983125, 285.316954889),(-176.335575688, 242.705098312),(-242.705098312, 176.335575688),(-285.316954889, 92.7050983125),(-300.0, 0.0),(-285.316954889,-92.7050983125),(-242.705098312, -176.335575688),(-176.335575688,-242.705098312),(-92.7050983125, -285.316954889),(0.0, -300.0),(92.7050983125, -285.316954889),(176.335575688, -242.705098312),(242.705098312, -176.335575688),(285.316954889, -92.7050983125),(300.0, 0.0)]

y = 0

for x in a:
 for a in range(20): <----- a is an integer
   t.goto(a[y])
   t.goto(0,0)
   y = y + 1

a从列表开始,但是在第二个for循环中,它成为一个整数。第t.goto(a[y])行正在尝试访问整数a变量。要解决此问题,只需将第二个a重命名如下:

a = [(285.316954889, 92.7050983125),(242.705098312,176.335575688),(176.335575688, 242.705098312),(92.7050983125,285.316954889),(0.0, 300.0),(-92.7050983125, 285.316954889),(-176.335575688, 242.705098312),(-242.705098312, 176.335575688),(-285.316954889, 92.7050983125),(-300.0, 0.0),(-285.316954889,-92.7050983125),(-242.705098312, -176.335575688),(-176.335575688,-242.705098312),(-92.7050983125, -285.316954889),(0.0, -300.0),(92.7050983125, -285.316954889),(176.335575688, -242.705098312),(242.705098312, -176.335575688),(285.316954889, -92.7050983125),(300.0, 0.0)]

y = 0

for x in a:
 for b in range(20): <---- note the use of b
   t.goto(a[y])
   t.goto(0,0)
   y = y + 1

答案 1 :(得分:0)

  

但是它仍然不起作用

避免混乱的索引编制,只需遍历数组的内容即可

import turtle as t

a = [(285.316954889, 92.7050983125), (242.705098312, 176.335575688),
     (176.335575688, 242.705098312), (92.7050983125, 285.316954889), (0.0, 300.0),
     (-92.7050983125, 285.316954889), (-176.335575688, 242.705098312),
     (-242.705098312, 176.335575688), (-285.316954889, 92.7050983125), (-300.0, 0.0),
     (-285.316954889, -92.7050983125), (-242.705098312, -176.335575688),
     (-176.335575688, -242.705098312), (-92.7050983125, -285.316954889), (0.0, -300.0),
     (92.7050983125, -285.316954889), (176.335575688, -242.705098312),
     (242.705098312, -176.335575688), (285.316954889, -92.7050983125), (300.0, 0.0)
]

t.penup()

for position in a:
    t.setheading(t.towards(position))
    t.goto(position)
    t.pendown()

t.exitonclick()