0

Let's say I want to create multiple turtles but also let the user decide how many there is. Is there a way to make a code like this? :

n = input()

for i in range(n):
   'turtle' + i = Turtle()

So you would end up with n turtles called 'turtle1', 'turtle2', ... 'turtle n'

2
  • 2
    You should use a list to store the turtle objects. Commented Jun 22, 2022 at 3:21
  • This question is a duplicate of stackoverflow.com/questions/4010840/… Commented Jun 22, 2022 at 4:34

3 Answers 3

1

Python comes with the possibility to execute commands given as strings if passed as argument to the exec() function. The following code does though what you asked for:

from turtle import Turtle
n = 5
for i in range(n):
   exec('turtle' + str(i) + ' = Turtle()')

print(turtle4) # gives <turtle.Turtle object at 0x............>

but it would be much better if you don't use it this way in your code and use a list for this purpose.

from turtle import Turtle
n = 5
turtle_list = []
for i in range(n):
   turtle_list.append(Turtle())

print(turtle_list[4]) # gives <turtle.Turtle object at 0x............>

Check out generating variable names on fly in python for more on this subject.

Sign up to request clarification or add additional context in comments.

4 Comments

This works, but is in overwhelming majority of cases a bad practice.
@Amadan Yes, you are right. You should avoid such a way of coding if possible. But ... the question is a question as it is. The OP didn't ask for advice on good practice but for a way how to accomplish what is to achieve. And the other responses mention already the possibility of using a list for storing the objects.
I know. I did not downvote you (or rather, I did, then retracted when you edited eval to exec). It is a valid answer. I just think any answer that includes one of those functions must have a disclaimer with it. Just like if someone asked "I am having trouble eating my steak with this chainsaw", giving chainsaw tips without noting that chainsaws are not normally used for eating steak is kind of irresponsible — you should really hint to the poor slob that they should consider a knife instead. :)
@Amadan I voted for closing this question as a duplicate ... and followed your advice to add a disclaimer.
1

You shouldn't assign them to unique variables that way, but you could always just store them in a list:

n = int(input())
turtles = []

for i in range(n):
   turtles.append(Turtle())

Then you could perform actions on specific turtles by calling on them by their index:

turtles[0].forward(100)
turtles[1].left(20)

1 Comment

Better will be to say: You shouldn't assign them to unique variables that way. instead of You wouldn't be able to assign them to unique variables that way, because the latter it's not really true.
0

problems with the code

  • input() returns a string and range() does not take strings.

  • "turtle"+i returns a string object which is immutable so you cant do something like "apple" = "banana"

solution

you can use a list to store the turtle objects in and you can use int() to cast the result from input() into a int.

import turtle
turtles = []
n = int(input())# convert input() into a int
for turtle in range(n):
    turtles.append(Turtle())# append a turtle to the list each iteration

you can access each turtle with its index like this

turtles[0]# first turtle
turtles[1]# second turtle

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.