Question

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

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:


SAMPLE INPUT AND OUTPUT:

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...
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
  • Complete the code The instructions to a turtle can be encoded in a string. Complete Asn4_5 as fo...

    complete the code 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. The first prompt will ask for a color. If the user enters quit or the user enters Quit the program will exit. The second prompt...

  • C Code Create a tool in which a user can enter a string (up to 25...

    C Code Create a tool in which a user can enter a string (up to 25 characters) and choose how they wish to manipulate the string from a menu your program will display (see the run-through). The program will continue to ask for commands until the user enters the “quit” command. The commands are: • “Replace All” If a user types “replace all” using ANY sort of capitalization, your program will prompt the user to enter the character to change...

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

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

  • I am supposed to reduce redundancy in the code and also make unknown inputs, output "unknown"....

    I am supposed to reduce redundancy in the code and also make unknown inputs, output "unknown". Step 1: Your development manager likes your program but is concerned about code duplication. Roger left a note on your desk, asking about using a feature for automatically converting strings to enumerations. Change your ToCommand method to employ this JAVA library method. (Hint: Look for a valueOf method). After converting your ToCommand method, run the program and enter an invalid command (one that does...

  • Write the code in python programming Language String Statistics: Write a program that reads a string...

    Write the code in python programming Language String Statistics: Write a program that reads a string from the user and displays the following information about the string: (a) the length of the string, (b) a histogram detailing the number of occurrences of each vowel in the string (details provided below), (c) the number of times the first character of the string occurs throughout the entire string, (d) the number of times the last character of the string occurs throughout the...

  • Step 1: design and code a program that uses a recursive method to convert a String...

    Step 1: design and code a program that uses a recursive method to convert a String of digit characters into an integer variable. For example, convert the character string “13531” to an integer with the value 13,531. Note that the comma is present only to distinguish the integer value from the character string (in quotes) . The program should be repetitive to allow the user to enter any number of number strings, convert them, display them, and sum them. When...

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

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

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

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