-6

i wrote this code to draw circles and hexagons.

import turtle
t = turtle.Turtle()
t.shape("turtle")
for(int i=1; i<=6; i++){
t.circle(100)
t.forward(100)
t.left(60)
t.forward(100)
t.left(60)
t.forward(100)
t.left(60)
t.forward(100)
t.left(60)
t.forward(100)
t.left(60)
t.forward(100)
}

i made "int i" to iterate but debugging says it's invalid syntax. why? i'm not native english speaker, so if you could please tell me easily. i'll be really appreciated.

June
  • 11
  • 3

2 Answers2

1

you want to use a proper python loop with range:

for _ in range(6):
    t.circle(100)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)

Since your loop variable is unused, you can use _ to "anonymize" it.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Python version

import turtle


t = turtle.Turtle()
t.shape("turtle")

for _ in xrange(6):
    t.circle(100)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)
    t.left(60)
    t.forward(100)

UPD: You should use range instead of xrange if you are using Python 3.

To find out which version of Python you have you can execute python --version

You can also read about Python loops over here https://wiki.python.org/moin/ForLoop

vovaminiof
  • 511
  • 1
  • 4
  • 13