1

I'm wanting to assign turtle commands as such:

F = turt.forward(30)
+ = turt.left(90)
- = turt.right(90)

however it won't let me assign these commands to the + and -, I don't want this to execute the commands. I also don't know how to execute them though. I want to use these variables to then right something like "F+F-F++FF"

import turtle
f = turt.forward(30)
+ = turt.left(90)
- = turt.right(90)
F+F-F-FF+F

1 Answer 1

2

First, you need to define some functions that we will use to define the operations of your turtle object:

def forward():
    turt.forward(30)

def left():
    turt.left(90)

def right():
    turt.right(90)

Next, we need to create a dictionary mapping the string keys to the functions that we had just defined:

map = {'F': forward, '+': left, '-'; right} # F will cause the turtle to go forward by 30

Lasty, we have to iterate over a sequence of strings in order to call functions according to this map:

for command in 'F+F-F++FF':
   map[command]()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I forgot about using dictionaries. That works perfectly.

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.