user5992911
user5992911

Reputation:

Position of Turtle always showing (0, 0)

I'm a beginner to Python programming (and programming in general) and was just playing around with the Turtle module in Python and was wondering why the coordinates of the turtle using turtle.pos() in the below program always gives (0, 0) after moving the turtle, when displayed in the graphical interface? This isn't the case when using the Python shell to move the turtle with the same turtle.forward()?

import turtle

wn = turtle.Screen()
wn.bgcolor("lightgreen")

def draw_lines(w, height):
    w.begin_fill()
    w.right(90)
    w.forward(height)
    w.write(turtle.pos())

line = turtle.Turtle()
line.color("blue", "red")
line.pensize(3)

height = [50]

for x in height:
    draw_lines(line, x)

wn.mainloop()

Thank you!

Upvotes: 1

Views: 910

Answers (2)

Juan Leni
Juan Leni

Reputation: 7588

Your turtle is passed as an argument (w) to draw_lines:

Correct draw_lines, it is easier if you use better names in your arguments:

def draw_lines(my_turtle, height):
    my_turtle.begin_fill()
    my_turtle.right(90)
    my_turtle.forward(height)
    my_turtle.write(my_turtle.pos())

Upvotes: 0

6502
6502

Reputation: 114461

The line

w.write(turtle.pos())

should be

w.write(w.pos())

Upvotes: 1

Related Questions