In: Computer Science
PYTHON: Turtle Command Interpreter
You will write a program that reads a sequence of codes, converts
them into Turtle commands, and then executes them. The codes are in
a Python list, passed as an argument to your function.
turtleRunner(t, x)
t is a Turtle
x is a list of Turtle commands
(f, d) | Move forward by d pixels |
---|---|
u | Lift pen up |
d | Put pen down |
(l, d) | Turn left by d degrees |
(r, d) | Turn right by d degrees |
(c, s) | Change color to "s" |
(w, d) | Change pen width to d |
For example [("f", 100), ("r", 30), ("c", "red"), "u", ("f",
50), ("r", 60), "d", ("f", 100)] will execute:
t.forward(100)
t.right(30)
t.color("red")
t.penup()
t.forward(50)
t.right(60)
t.pendown()
t.forward(100)
Your program will need to do the following:
x =
[("w",3),("c","red"),("f",100),("r",120),("c","blue"),("f",100),("r",120),("c","green"),("f",100)]
turtleRunner(t, x)
Draws the shape below:
isinstance(x, y) returns True if x is an instance of class y. So
isinstance(e, tuple) will return True if e is a Tuple.
(Note no quotes around tuple in the call)
isinstance(e, str) will return True if e is a String.
Starter code(please by off this):
# Tkinter
#
# Turtle Command Interpreter
#
# You will write a program that reads a list of codes, converts
them into Turtle commands, and then executes them.
#
# Code list:
# (f, d) Move forward by d pixels
# u Up
# d Down
# (l, d) Turn left by d degrees
# (r, d) Turn right by d degrees
# (c, s) Change color to "s"
# (w, d) Change width to d
#
# Example:
# [("f", 100), ("r", 30), ("c", "red"), "u", ("f", 50), ("r", 60),
"d", ("f", 100)]
#
# This will execute:
# t.forward(100)
# t.right(30)
# t.color("red")
# t.penup()
# t.forward(50)
# t.right(60)
# t.pendown()
# t.forward(100)
#
# Your program will need to do the following:
# 1) Define a function named turtleRunner(x), where x is a
list
# 2) Go through each element of x one at a time.
# 3) If the element is a tuple (if isinstance(e, tuple)):
# a) Determine if the code is f, r, or c
# b) Execute the correct turtle command using the
second part of the tuple (a distance or color)
# 4) If the element is a string (if isinstance(e, str)):
# a) Execute the correct turtle command
#
import turtle
def turtleRunner(t, x):
pass
w = turtle.Screen()
t = turtle.Turtle()
x =
[("w",3),("c","red"),("f",100),("r",120),("c","blue"),("f",100),("r",120),("c","green"),("f",100)]
turtleRunner(t, x)
import turtle
#Executes all the statements present in list x
def turtleRunner(t, x):
#For loop to access every element of the list
for e in x:
#if element e is tuple
if isinstance(e,tuple):
if e[0] == "f":
t.forward(e[1])
elif e[0] == "r":
t.right(e[1])
elif e[0] == "c":
t.color(e[1])
elif e[0] == "c":
t.color(e[1])
elif e[0] == "l":
t.left(e[1])
elif e[0] == "w":
t.width(e[1])
#if element e is string
elif isinstance(e,str):
if e == "u":
t.up()
elif e == "d":
t.down()
w = turtle.Screen()
t = turtle.Turtle()
x =
[("w",3),("c","red"),("f",100),("r",120),("c","blue"),("f",100),("r",120),("c","green"),("f",100)]
turtleRunner(t, x)
#to stop the screen from immediately closing
turtle.done()
------------------------------------------------------------------------------------------
Note: Code has been commented for better understanding.
Code snippet for indentation purposes:
import turtle
#Executes all the statements present in list x
def turtleRunner(t, x):
#For loop to access every element of the list
for e in x:
#if element e is tuple
if isinstance(e,tuple):
if e[0] == "f":
t.forward(e[1])
elif e[0] == "r":
t.right(e[1])
elif e[0] == "c":
t.color(e[1])
elif e[0] == "c":
t.color(e[1])
elif e[0] == "l":
t.left(e[1])
elif e[0] == "w":
t.width(e[1])
#if element e is string
elif isinstance(e,str):
if e == "u":
t.up()
elif e == "d":
t.down()
w = turtle.Screen()
t = turtle.Turtle()
x = [("w",3),("c","red"),("f",100),("r",120),("c","blue"),("f",100),("r",120),("c","green"),("f",100)]
turtleRunner(t, x)
#to stop the screen from immediately closing
turtle.done()
Output:
----------------------------------------------------------------------------------------
Hope this helps. If you like the answer, please upvote. Cheers!!