Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program, you are wandering through the forest; you are lost! Happily, youve brought with you a ball of twine, that you unroll as

In this program, you are wandering through the forest; you are lost! Happily, youve brought with you a ball of twine, that you unroll as you go. That way, you can always get back to where you started. (Sadly, theres no way out of the forest. Sorry about that!) In this program, you will model this as a small interactive game. You will prompt the user with your current state, and then they will type a command; based on that command, you will update the state, print out some sort of response, and then prompt them for another command. The commands that you must support include: n,s,e,w (move one square in that direction), back (retrace your steps by one space), crossings (tell the user how many times theyve been at the current location), map (print a map of the space youve travelled), and ranges (give the x and y bounds of your path so far). Note that none of these commands have any parameters.

image text in transcribed

image text in transcribed

image text in transcribed

python please

3.1 The History You will model the twine using a stack, and the stack must be implemented as a Python list; each element in the stack must be an (x,y) tuple, giving your position at that point in the path. If the user gives the back command, you must pop the most recent value off of the stack, effectively moving you backwards in time. Your start position is always (0,0), and it must always be included, in the first element of the stack; you must not pop it off. 3.2 The First Question Before the very first prompt of the game (at the very beginning of your program) print() the following message. Please give the name of the obstacles filename, or for none: Then, get one input() from the user (but ignore it completely). Once the rest of your program is working, you will update this, adding the Obstacles feature, which is documented at the bottom of this spec. 3.3 Prompt The prompt, before every command, must be exactly three lines, like this: Current position: (3,2) Your history: [(0,0), (1,0), (2,0), (3,0), (3,1), (3,2)] What is your next command? and after you have printed out the response to any command, you must add a blank line before the next prompt. (To make the output from GradeScope look a little nicer, please print out the third line of the prompt using print() - and then use input(), with no argument, to read the command.) 3.4 Errors If the user types any command which is not valid, print out an error message. For this program, error messages must begin with ERROR: and the autograder will ignore the rest of the message. However, your TA will inspect your code to see if you are printing out useful error messages, which would help the user. You must detect the following user errors: The user types more than one word on the command line. Since no com- mands take any parameters, this is an error. The user types a command which you don't recognize. 2 (When you implement Obstacles): The obstacles filename is blank (When you implement Obstacles): The obstacles filename doesn't exist (When you implement Obstacles): The obstacles file has invalid data in- side it (When you implement Obstacles): The obstacles filename is blank (When you implement Obstacles): The obstacles filename doesn't exist (When you implement Obstacles): The obstacles file has invalid data in- side it 3.5 EOF When the autograder runs your program, it will hook up a file to the input of your program. If you call input() when you are already at the end of that input file, Python will throw an EOFError. (EOF stands for End Of File.) While we are not going to learn about exceptions in depth yet, I would like you to use the following code snippet: = try: user_data input() except: do_something_here This code protects you from EOFError. If you are able to read from the user, then the user_data variable gets set, and you just move on to the next block of code. But if an exception occurs, then it will jump you into the except block - and in there, you can do whatever you want to handle the error. (For instance, you might want to break the loop that you're in.) 3.1 The History You will model the twine using a stack, and the stack must be implemented as a Python list; each element in the stack must be an (x,y) tuple, giving your position at that point in the path. If the user gives the back command, you must pop the most recent value off of the stack, effectively moving you backwards in time. Your start position is always (0,0), and it must always be included, in the first element of the stack; you must not pop it off. 3.2 The First Question Before the very first prompt of the game (at the very beginning of your program) print() the following message. Please give the name of the obstacles filename, or for none: Then, get one input() from the user (but ignore it completely). Once the rest of your program is working, you will update this, adding the Obstacles feature, which is documented at the bottom of this spec. 3.3 Prompt The prompt, before every command, must be exactly three lines, like this: Current position: (3,2) Your history: [(0,0), (1,0), (2,0), (3,0), (3,1), (3,2)] What is your next command? and after you have printed out the response to any command, you must add a blank line before the next prompt. (To make the output from GradeScope look a little nicer, please print out the third line of the prompt using print() - and then use input(), with no argument, to read the command.) 3.4 Errors If the user types any command which is not valid, print out an error message. For this program, error messages must begin with ERROR: and the autograder will ignore the rest of the message. However, your TA will inspect your code to see if you are printing out useful error messages, which would help the user. You must detect the following user errors: The user types more than one word on the command line. Since no com- mands take any parameters, this is an error. The user types a command which you don't recognize. 2 (When you implement Obstacles): The obstacles filename is blank (When you implement Obstacles): The obstacles filename doesn't exist (When you implement Obstacles): The obstacles file has invalid data in- side it (When you implement Obstacles): The obstacles filename is blank (When you implement Obstacles): The obstacles filename doesn't exist (When you implement Obstacles): The obstacles file has invalid data in- side it 3.5 EOF When the autograder runs your program, it will hook up a file to the input of your program. If you call input() when you are already at the end of that input file, Python will throw an EOFError. (EOF stands for End Of File.) While we are not going to learn about exceptions in depth yet, I would like you to use the following code snippet: = try: user_data input() except: do_something_here This code protects you from EOFError. If you are able to read from the user, then the user_data variable gets set, and you just move on to the next block of code. But if an exception occurs, then it will jump you into the except block - and in there, you can do whatever you want to handle the error. (For instance, you might want to break the loop that you're in.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Lakehouse

Authors: Bill Inmon ,Mary Levins ,Ranjeet Srivastava

1st Edition

1634629663, 978-1634629669

More Books

Students also viewed these Databases questions

Question

What is the relationship between humans and nature?

Answered: 1 week ago