Question

Complete the code The instructions to a turtle can be encoded in a string. Complete Asn4_5 as fo...

complete the code

  1. The instructions to a turtle can be encoded in a string. Complete Asn4_5 as follows:

In main() the user will be prompted to enter instructions to a turtle as follows. The instructions will be entered in a loop that will end when the user enters quit or Quit at the first prompt.

  1. The first prompt will ask for a color. If the user enters quit or the user enters Quit the program will exit.
  2. The second prompt will ask the user to enter a sequence of commands, separated by commas. Commands may be in uppercase or lowercase and will be in the form of cd*, where c represents a one-character turn, move, or pen command followed by zero or more digits, as described below:
    1. If the command is U or the command is D, it means pick the pen up or put it down; it is not followed by any digits.
    2. If the command is L or the command is R, it will be followed by an integer that determines the number of degrees to turn.
    3. If the command is F or the command is B, it will be followed by an integer that determines the distance to move forward or backward.

After the user enters the command string, main() will split the string into a list of commands. For each command, it will call move_turtle(t, cmd), where t is a turtle and cmd.

Save your Asn4 folder, with the completed programs, by 5 minutes after class time on the due date.

The following shows an interaction in which the user moved the turtle back 100 units, drew a red square, then moved the turtle forward 100 units and drew a green diamond. Note that the user can enter upper or lowercase commands. Also, notice that the command string does not have any spaces.

Please enter a color: red

Enter commands separated by commas: U,B100,D

Please enter a color: red

Enter commands separated by commas: L90,F50,L90,F50,L90,F50,L90,F50

Please enter a color: red

Enter commands separated by commas: U,f100,D

Please enter a color: green

Enter commands separated by commas: L45,f50,L90,F50,L90,F50,L90,F50,R45

Please enter a color: Quit

import turtle

def move_turtle(t, move):
'''
Moves turtle t according to move; t is a turtle;
move is a string where the first character is a command.
If the string is longer than one character, the remaining
characters are digits. Those digits are an integer that
determines either number of degrees to turn or number of
units to move, depending on the command
'''
#Convert move to upper case
#Get the command from move - it's the first character
#If the command is U, pick up the pen
#Else if the command is D, put down the pen
#Else if the command is L
#Get the angle from move - it's all the characters after the first one
#Make the angle an integer
#Turn left angle degrees
#Else if the command is R
#Get the angle from move - it's all the characters after the first one
#Make the angle an integer
#Turn right angle degrees
#Else if command if F
#Get the distance from move
#Make the distance an integer
#Move forward distance units
#Else if the command is B
#Get the distance from move
#Make the distance an integer
#Move backward distance units
pass

def main():

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THE CODE FOR THE ABOVE QUESTION IS:

import turtle


def move_turtle(t, move):
    # coverting to uppercase
    move = move.upper()

    # extracting the command
    c = move[0]
  
    # executing the respective command using if-elif-else
    if c == 'U':
        t.up()                # picking up the turtle's pen
    elif c == 'D':
        t.down()              # putting down the turtle's pen
    elif c == 'L':
        d = int(move[1:])     # extracting the decimal number
        t.left(d)             # moving the turtle left
    elif c == 'R':
        d = int(move[1:])     # extracting the decimal number
        t.right(d)            # moving the turtle right
    elif c == 'F':
        d = int(move[1:])     # extracting the decimal number
        t.forward(d)          # moving the turtle forward
    elif c == 'B':
        d = int(move[1:])     # extracting the decimal number
        t.backward(d)         # moving the turtle backward
    else:
        pass


def main():
    color = ''
    myTurtle = turtle.Turtle() # creating a turtle object
  
    while color != 'quit' or color != 'Quit':
    
        color = input('Please enter a color: ')
        if color == 'quit' or color == 'Quit':
            break
        myTurtle.color(color)   # setting color of the turtle
      
        # taking the commands as input and splittin them by comma
        commands = input('Enter commands separated by commas: ').split(',')
      
        # looping each command and executing it one by one
        for command in commands:
            move_turtle(myTurtle, command)


if __name__ == '__main__':
    main()

CODE SCREENSHOT FOR UNDERSTANDING INDENTATION:
1 import turtle 4 def move_turtle(t, move): # coverting to uppercase move move.upper() 6 # extracting the command cmove[e] 9


SAMPLE INPUT AND OUTPUT:
Powered by trinket Please enter a color: red Enter commands separated by commas: U,B100,D Please enter a color red Enter comm

Add a comment
Know the answer?
Add Answer to:
Complete the code The instructions to a turtle can be encoded in a string. Complete Asn4_5 as fo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Writing the code in IDLE. Make sure the code is working. thank you so much Create the following turtle pattern. Using the turtle and random modules, write a program that creates an output as...

    Writing the code in IDLE. Make sure the code is working. thank you so much Create the following turtle pattern. Using the turtle and random modules, write a program that creates an output as the one shown in the picture below. Consider the following: . Change the color of the window to "red" using wn.bgcolor property of the screen variable you create. . Your turtle variable has to have a pen color "white", you have to use the .pencolor property...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

  • PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do...

    PYTHON PROGRAMMING Instructions: You are responsible for writing a program that allows a user to do one of five things at a time: 1. Add students to database. • There should be no duplicate students. If student already exists, do not add the data again; print a message informing student already exists in database. • Enter name, age, and address. 2. Search for students in the database by name. • User enters student name to search • Show student name,...

  • Objectives By the end of this program, the student will have demonstrated the ability to Write...

    Objectives By the end of this program, the student will have demonstrated the ability to Write static methods Use methods in a different class Write a sentinel-controlled loop Write nested if/else statements Manipulate Strings by character position StringUtils You are to complete the class StringUtils, which is in the attached zip file. Add the following public static methods: copy Write a method String copy(String currentString, int startPosition, int onePastLastPosition). This method returns a substring of currentString, including startPosition, but ending...

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • The goal is to create a code for implementing a Columns game using pygame Your program...

    The goal is to create a code for implementing a Columns game using pygame Your program will read its input via the Python shell (i.e., using the built-in input() function), printing no prompts to a user with no extraneous output other than precisely what is specified below. The intent here is not to write a user-friendly user interface; what you're actually doing is building a tool for testing your game mechanics, which we'll then be using to automatically test them....

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT