aclem
aclem

Reputation: 1

Can you use the name of a turtle in the parameters of a variable?

import turtle as trtl
def position(hold):
  hold.forward(200)

position('trtl')

I'm trying to make a program which has multiple turtles use a similar function between all of them, is something like what is shown in the image possible?

Upvotes: 0

Views: 249

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295325

If you want to refer to things by name, store them in a dict; use the name as keys.

import turtle

turtles = {
  "one": turtle.Turtle(),
  "two": turtle.Turtle(),
}

def position(turtle_name):
  return turtles[turtle_name].forward(200)

position('one')

...but it's unclear why you'd do that at all instead of...

import turtle as turtle_mod

turtle_one = turtle_mod.Turtle()
turtle_two = turtle_mod.Turtle()

def position(turtle):
  return turtle.forward(200)

position(turtle_one)

Upvotes: 1

Related Questions