Is there way to create multiple turtles using a loop?

Question:

For example:

import turtle

num = 1
for i in range (10):
    num = turtle.Turtle()
    num += 1

I want to make 10 turtles named from 1 to 10. A code like this just makes one turtle called num before kicking up an error (TypeError: unsupported operand type(s) for +=: ‘Turtle’ and ‘int’). Is there a way to make the 10 turtles named from 1 to 10 without manually defining them all?

Asked By: Jalco28

||

Answers:

num = []
for i in range (10):
    num.append(turtle.Turtle())

?

Answered By: Błotosmętek
import turtle

# Create a list of turtles
turtles = [turtle.Turtle() for _ in range(10)]
Answered By: MeBex

Instead of num = 1, it should be num = 0, but appending works better because num is just simply counting in this case and doesn’t have a relationship with num = turtle.Turtle().

Answered By: Alden_Ryabyuk
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.