-1

I can't execute this code because of a TypeError in the next line

t.goto(a[y]) (I'm a starter btw so please elaborate as much as possible)

    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
1
  • 1
    Can you post the complete script? Also, could you please explain what would be the expected output?
    – alec_djinn
    Jun 11, 2019 at 14:13

2 Answers 2

1

This error is because you have 2 definitions for 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 a in range(20): <----- a is an integer
   t.goto(a[y])
   t.goto(0,0)
   y = y + 1

a starts as a list, but in the second for loop, becomes an integer. The line t.goto(a[y]) is attempting to access the integer a variable. To fix this, simply rename the second a as follows:

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
2
  • "does not work" is not helpful. Tell us what is actually happening. Jun 11, 2019 at 14:16
  • I'm assuming the second for loop has a purpose, but the code you gave does not reflect this. It looks like a drawing type problem where it goes to each point, and then back to (0,0). The second loop just causes this to repeat 20 times before moving to the next point.
    – Jack Walsh
    Jun 11, 2019 at 14:30
0

but it still does not work

Avoid the messy indexing and simply loop over the content of your array:

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.