User Interface

menu.py:14 points


Objective

In this step we’ll create the text-based user interface that will allow users to interact with your program—i.e, the main menu. Over the course of this semester, you’ve practiced implementing a number of menus already, so by now you’re familiar with the basic requirements. At a high level, we need our menu do three things:

  • Display a list of options to the user.
  • Get the user’s input indicating which option they want.
  • Check to make sure the input makes sense.

Since this has come up more than once, it would be great if we could make a more general solution that could be reused and extended in a range of different scenarios. With this goal in mind, we’ll build a simple Menu class to help manage interactions with the user.

How to Complete This Section

  • As before, read over the Menu Class Summary below to get a high-level view of how it works.
  • Next, read carefully through the Detailed Description of Methods and implement each method according to the specifications provided.
  • Note that a simple test case has been written for you already. You should run python3 menu.py periodically as you go to check your progress.

Attributes

  • intro (str): A short message displayed before the list of available options.
  • options (dict): A dictionary containing all valid user inputs (stored as keys) and descriptions of available options associated with each input (stored as values). An example dictionary called walley_options is provided in the test_menu function.

Methods

  • __init__
  • get_valid_input
  • __str__

Detailed Description of Methods

This section contains the requirements for each method your DataSet class will need to include, as well as some implementation suggestions. (Note: These are listed below in the order we recommend completing them, which is slightly different from the order they appear in the Menu class.)

__init__(self, intro, options)

Initialize all attributes to the appropriate values. In this case, you only need to assign the two parameters to their corresponding object attributes.

__str__(self)

This method should return a string that contains the menu intro along with a list of the options stored in the options dictionary. Each option should be listed on a single indented line below the intro. Lines describing options should be of the form <user_input>: <option_description>, where <user_input> indicates what the user is expected to enter to select the option, and <option_description> is the corresponding description the explains what is being selected.

For example, if intro is assigned to the string This is an intro message! and <options> is the dictionary {"a": "Description of option a", "b": Description of option b">, then __str__ should return the following string:

This is an intro message!
    a: Description of option a 
    b: Description of option b

where “a” and “b” represent expected user input.

get_valid_input(self,prompt)

This method provides the core functionality of the Menu. It should do all of the following:

  1. Display the menu to the user. (Hint: the __str__ method above can help you do this.)
  2. Ask the user for input using the message assigned to the variable prompt. (Since prompt is a parameter, its value will be assigned when the method is called.)
  3. Check the user’s response to make sure it is valid.
    • If the input is not valid:
      • Display a message that shows the invalid input that the user entered, explains that it is not valid, and asks the user to try again.
      • For example: 'hurdydurdy' is not a valid selection. Please try again.
      • Repeat from step 1 to let the user try again. (An invalid choice should not cause the program to exit.)
    • If the input is valid:
      • Display a message to the user confirm their selection.
      • The format of the message should be You selected: <description>, where <description> is the short description given in the options dictionary.
      • After displaying the confirmation, the method should return the (valid) user input.

Testing

As mentioned above, a simple regression test has been implemented for you in the test_menu function, which will be executed when you run python3 menu.py. Below is an example of terminal output from a correctly implemented Menu class in which the user first chooses and invalid option, then chooses a valid option.

Welcome to Walley World!
    z: Sing Zippity Doo Dah.
    m: Meet Marty Moose.
    b: Purchase a BB gun.
Enter the letter corresponding to your selection: 0
'0' is not a valid selection. Please try again.
Enter the letter corresponding to your selection: b
You selected: Purchase a BB gun.
Validated user input:  b