Jacob Boughton
Jacob Boughton

Reputation: 172

Can you name a turtle in python based of a variable?

Sorry, nooby question but couldn't find anywhere if it was possible to name a turtle with a variable in its name?

For example:

Variable = 1

Turtle(Variable) = turtle.Turtle()

Turtle1.goto(0,0)

Thanks in advance :)

Upvotes: 0

Views: 847

Answers (2)

cdlane
cdlane

Reputation: 41872

This can be emulated with a dict or a list (as @UsernameObfuscation notes, +1):

from turtle import Screen, Turtle

variable = 1

turtles = {}  # or turtles = [] if 'variable' is only an incrementing int

turtles[variable] = Turtle()

turtles[variable].goto(100, 100)

screen = Screen()
screen.exitonclick()

Upvotes: 1

Hook
Hook

Reputation: 354

The line Turtle(Variable) = turtle.Turtle() is problematic from another point of view.

You are doing Left Hand Side assignment to a variable called Turtle, and you are giving it the value of turtle.Turtle(), so basically you suppose to get syntax error for that line.

On the other hand, the next code should give you the result you are looking for, but without naming the turtle:

Turtle = turtle.Turtle()
Turtle.goto(0,0)

Upvotes: 0

Related Questions