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'

Claudio
  • 7,474
  • 3
  • 18
  • 48

3 Answers3

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)
okayatcp12
  • 308
  • 1
  • 9
  • 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. – Claudio Jun 22 '22 at 04:29
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.

Claudio
  • 7,474
  • 3
  • 18
  • 48
  • This works, but is in overwhelming majority of cases a bad practice. – Amadan Jun 22 '22 at 04:13
  • @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. – Claudio Jun 22 '22 at 04:23
  • 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 Jun 22 '22 at 04:32
  • @Amadan I voted for closing this question as a duplicate ... and followed your advice to add a disclaimer. – Claudio Jun 22 '22 at 04:48
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