Question

In: Computer Science

PYTHON: Turtle Command Interpreter You will write a program that reads a sequence of codes, converts...

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

Code List
(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:

  1. Define a function named turtleRunner(t, x), where t is a turtle and 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)):
    1. Determine if the code is f, r, c, or w
    2. 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)):
    1. Execute the correct turtle command

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)

Solutions

Expert Solution

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!!


Related Solutions

Turtle Command Interpreter You will write a program that reads a sequence of codes, converts them...
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 Code List (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...
Write a program that reads the letter codes of a telephone number pad and converts them...
Write a program that reads the letter codes of a telephone number pad and converts them into their corresponding number patterns. a.        One acceptable form of output: Sample input:              Sample Output: 800-MATTRESS 800-628-8737 (leave off the last “S”) 800-mattress                800-628-8737 (leave off the last “S”) Another acceptable form of output: Sample input:              Sample Output:             8   8 0   0   0 0    M 6 A 2 T                                8 T                                8 R                                7 E                                 3 S                                 7 The letters ‘A’ to ‘Z’ and ‘a’ to...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
I need to write an interpreter for arithmetic expressions in Python. This program receives an input...
I need to write an interpreter for arithmetic expressions in Python. This program receives an input string with an arithmetic expression and does: 1. prints a ">>>" as if it was a terminal 2. lets us allocate a variable to a a numeric value (as "a = 3") then it stores {'a':3} to a dictionary memory 3. whenever it receives an expression or input, transform it to postfix notation and print it (even when allocating variables) 4. it is able...
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to...
PYTHON WHILE Write a program that prompts for and reads the number ? of spheres to be processed. If ?≤0 your program must display an error message and terminate; otherwise it does the following for ? times: Write a program that prompts for and reads the number ?n of spheres to be processed. If ?≤0n≤0 your program must display an error message and terminate; otherwise it does the following for ?n times: Prompts for and reads the volume of a...
Input the available amount of Rice in kg. Write a program that reads a sequence of...
Input the available amount of Rice in kg. Write a program that reads a sequence of Rice donations in kg and add them to the initially available amount of Rice. The loop should stop once the accumulated amount of Rice exceed 50 kg. Then, the program should print the final amount of Rice and the number of considered donations.
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance in Feet. The user should be able to enter a distance in Meters, click a button, and then see the equivalent distance in feet. Use the following formula to make the conversion: Meters = Feet x 0.304 For example, 1 Meter is 3.28 Feet.
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT