Python - Menu module assignment

From WLCS

Objective

  • You will create a Python module (not class!) that contains global variables and functions

Purpose

  • The Menu module allows you to use the variables and functions to obtain input commands for your game.

Filename

  • Put all the variables and function definitions in a file named menu.py
  • When you want to use the Menu module in other files, use the line: import menu at the top of the page

Variables

  • This is not a class, so all you need to do is create the variables with the following values below:
    • NORTH = 1
    • EAST = 2
    • SOUTH = 3
    • WEST = 4
    • ATTACK = 5
    • LOOK = 6
    • GET = 7
    • QUIT = 8
    • DO_NOT_UNDERSTAND = -1

Functions

The Menu module should define the following functions :

  • void printCommands(): This function will print out the available commands (north, east, look, attack, etc.) for the parse() function . This can print out each of the individual commands, or it can just print out a summary. See the sample execution run below (lines 6-8) for an example of how this function might work.
  • boolean askYesNoQuestion(String prompt): The parser should use input(prompt) to print prompt and store the result in a string. Check if the result is "yes", "y", "no", or "n" answers, and return True or False for yes or no answers, respectively. This function should not care about capitalization, so "YES" and "yes" are both valid inputs. If the function does not understand the input, it should print "Please answer yes or no.", and ask for the user to input again (HINT: You need a loop!).
  • int parse(): This function does a few things:
    1. Prompt the user for input (this can be a simple "enter next command" using input("enter command");
    2. Compare the input to a series of strings for each of the above constants. If the input contains the user's typed command "north" or "n", the parse() function should return the integer constant NORTH. If the user types "attack", the function should return ATTACK, if the user types "look" the function returns LOOK, "get" for GET, and so on. We don't care about case, so you should use the string's upper() function or lower() function . If the user input does not match any known command, the parse() function should return DO_NOT_UNDERSTAND.

Note: If the first letter of the user's input line matches, then you should still return the appropriate value (so "l" is valid for "look", as is "large" and "LAB"). To get the first letter of a string s, you can use s[0]

Note: Both functions in your parser should ignore capitalization, so that for instance "N" returns the same value as "n", and "Attack" returns the same value as "attack"

Testing

To help you test your menu module, we have provided a ParserUsage.py file. You do not need to modify this file, but you should understand how it works. This file shows how to use the menu module, and tests your various functions. Below is an example of what a sample execution run of ParserUsage. Line numbers have been added so that it is easier to explain what is going on -- they are not output by the program.

1	This program will test the Parser class
2	
3	First, the parse() method will be tested
4	Enter q to quit
5
6	Valid commands are any of the four directions (north, south, east, west),
7	        look, attack, get, or quit
8	You can abbreviate any command by its first letter
9	
10	Enter next command: look
11	You entered look
12	Enter next command: LOOK
13	You entered look
14	Enter next command: l
15	You entered look
16	Enter next command: L
17	You entered look
18	Enter next command: LAB
19	You entered look
20	Enter next command: LaBoRaToRy
21	You entered look
22	Enter next command: q
23	You entered quit
24
25	Next, the askYesNoQuestion() will be tested
26	Do you like green eggs and ham?
27	maybe
28	Please answer yes or no
29	i don't know, sam i am
30	Please answer yes or no
31	perhaps
32	Please answer yes or no
33	YES
34
35	Result was true