turtle.Screen().turtles() function in Python

The turtle.Screen().turtles() function returns the list of all turtles currently on the screen. This is a method of the Screen object.

Syntax: turtle.turtles()
Parameters: None.

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

Demo53.py

# turtle.Screen().turtles() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()

all_turtles = window.turtles()  # Get list of all turtles
print("Number of turtles:", len(all_turtles))

window.exitonclick()

The following is the output:

turtle.Screen().turtles() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtles: t1 = turtle.Turtle(), t2 = turtle.Turtle(), t3 = turtle.Turtle() create three turtles.
  • Get all turtles: all_turtles = window.turtles() returns a list of all Turtle objects on the screen (includes t1, t2, t3).
  • Print count: print(“Number of turtles:”, len(all_turtles)) outputs how many turtles exist.
  • Close on click: window.exitonclick() waits for a mouse click, then closes the window.

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.bgpic() function in Python
turtle.setworldcoordinates() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment