0

For example, I have many turtles and I want them all move to forward. Should I repeat my forward command for each turtle or there is a Pythonic way?

import turtle
tx=turtle.Turtle()
ty=turtle.Turtle()
tz=turtle.Turtle()
ti=turtle.Turtle()
tj=turtle.Turtle()
tx.fd()
ty.fd()
tz.fd()
ti.fd()
tj.fd()

Is there any functions like map to use fd() method then write my objects name? I know it's better to use a list and the map function but I want to name my objects.

1
  • If you want the turtles to have names, why not use a dictionary? Commented Nov 23, 2013 at 21:24

2 Answers 2

4
turtles = [tx, ty, tz, ti, tj]
for turtle in turtles:
    turtle.fd()

I'd still recommend not giving them their own variables, but it's your call. Also, map is not a good idea for functions you want to execute for side effects; if you don't iterate over the whole map object somehow, the function won't get called on all the things you want it to be called on.

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

Comments

0

you can use dictionary for naming and use loop on the values:

import turtule
t_names = ['tx', 'ty', 'tz', 'ti', 'tj']
t = dict([(name, turtle.Turtle()) for name in t_names])
[one_t.fd() for one_t in t.values()]

and use them by name:

t['tz'].bw()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.