Question

In: Computer Science

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

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.


import turtle
t = turtle.Turtle(shape="arrow")
t.speed(5);
window = turtle.Screen()
window.setup (width=500, height=500)

def turtleRunner(t, x):
for command in x:
if isinstance(command, tuple):
commandMode = command[0]
if commandMode=="f":
commandVal = command[1]
t.forward(commandVal)
elif commandMode=="r":
commandVal = command[1]
t.right(commandVal)
elif commandMode=="l":
commandVal = command[1]
t.left(commandVal)
elif commandMode=="u":
t.penup()
elif commandMode=="d":
t.pendown()
elif commandMode=="c":
commandVal = command[1]
t.color(commandVal)
elif commandMode=="w":
commandVal = command[1]
t.pensize(commandVal)
  
  
  
  
  


x = [("w",3),("c","red"),("f",100),("r",120),("c","blue"),("f",100),("r",120),("c","green"),("f",100)]
turtleRunner(t, x)

window.bgcolor("white")
window.exitonclick()


Related Solutions

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,
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...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character β€œch” to the string β€œs”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write two versions of a program that reads from the user a sequence of positive integers...
Write two versions of a program that reads from the user a sequence of positive integers ending with -1, and another positive integer num that the user wishes to search for. The program should then print all the line numbers in sequence entered by the user, that contain num, or a message saying that num does not show at all in the sequence. Your program should interact with the user exactly as it shows in the following example: Please enter...
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
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'm trying to write a program that reads 3 heights of kids and puts them in...
I'm trying to write a program that reads 3 heights of kids and puts them in ascending order using if and else statements but i'm having trouble #include<stdio.h> int main() { int k1,k2,k3; printf("Enter height of kid #1 >"); scanf("%d",&k1); printf("Enter height of kid #2 >"); scanf("%d",&k2); printf("Enter height of kid #3 >"); scanf("%d",&k3); if(k3>k2>k1) { printf("In ascending order %d %d %d",k3,k2,k1); } else if(k3>k1>k2) { printf("In ascending order %d %d %d",k3,k1,k2); } else if(k2>k3>k1) { printf("In ascending order %d...
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT