turtle.ycor() function in Python

The turtle.ycor() function in Python returns the turtle’s current y-coordinate. Useful for getting the turtle’s position for calculations.

Syntax: turtle.ycor()
Parameters: None.

Let us see an example to implement the turtle.ycor() function in Python:

Demo17.py

# turtle.ycor() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

print("Initial y coordinate:", t.ycor())
t.left(90)
t.forward(100)
print("After moving north 100:", t.ycor())

window.exitonclick()

The following is the output:

turtle.ycor() function in Python

In the above code, we followed the below steps:

  1. Import the module: import turtle loads Python’s turtle graphics library.
  2. Create the screen: window = turtle.Screen() opens the drawing window.
  3. Create the turtle: t = turtle.Turtle() creates the turtle you’ll control.
  4. Print initial y: print(“Initial y coordinate:”, t.ycor()) outputs the turtle’s current y-position. By default, it starts at y = 0.
  5. Turn north: t.left(90) rotates the turtle 90° counterclockwise to face up (north) without moving.
  6. Move forward: t.forward(100) moves 100 units upward, increasing the y-coordinate and drawing a line since the pen is down by default.
  7. Print new y: print(“After moving north 100:”, t.ycor()) prints the updated y-position, which will be 100 if starting from the origin.
  8. Close on click: window.exitonclick() keeps the window open until you click, then closes.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.xcor() function in Python
turtle.up() or turtle.penup() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment