Description
Building our menu
The next step in our project is to build a menu and the code to support it. The menu provides the interface that a user will use to interact with the program.
This assignment will give us practice using loops and conditionals.
We are building on the previous assignment. So start with a copy Lab Assignment 2 and add your new code.
In this Lab Assignment you are going to:
Create a new file named lab_assignment 3.py.
Copy your code from Lab Assignment 2 and paste it into you new file
Add information to update the module docstring to reflect Lab Assignment 3
Add the new code you are writing to support the requirements for Lab Assignment 3
Delete the main() the code used in lab_assignment 2 and write a new main() function to generate the required output (the Unit Test, for Lab Assignment 3)
Write a function named print_menu()
print_menu() that takes no arguments and returns nothing, don’t forgat to add the docstring for the new function. Its whole job is to print this menu:
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
Incidentally, during the rest of the course, we will work on implementing all of the items referenced by the menu.
We are first going to write stub functions which will later be completed. A stub is a function that has the expected signature (i.e. name of the function and parameters), but an incomplete implementation. A stub function is used as a placeholder and so that the code that calls the function can be tested before the called function is fully written.
Next, we need stub functions for the following
Each line below gives you the function signature (name of the function and it’s parameters) of each function that you will create in the assignment
new_file(dataset) will be called when the user chooses item one.
choose_units() will be called when the user chooses item two.
change_filter(sensor_list, active_sensors) will be called when the user chooses item three.
print_summary_statistics(dataset, active_sensors) will be called when the user chooses item four.
print_temp_by_day_time(dataset, active_sensors) will be called when the user chooses item five.
print_histogram(dataset, active_sensors) will be called when the user chooses item six. This is an optional no-credit project.
And what about item seven? The program should exit gracefully.
These functions do not have any functionality yet. They will initially be stub functions, later you will add the code required for the application. Instead of using the pass statement, we will use a print statement for each function so we know that it has been called. Our new_file() function, for example, will look like this:
def new_file(dataset):
“””
Open a new file
“””
print(“New File Function Called”)
Since you do not know the purpose of each of these functions yet, you can just a generic docstring for now.
main()
In addition, to implementing these new functions, you need to create logic in the main() to print the menu, ask for the user’s input, and process the response. Your code should call the appropriate function and then print the menu again. This is the Unit Test for this Lab Assignment.
Some of these functions require parameters, but we haven’t defined the variables (arguments) that we need to pass to the functions. The interpreter will complain if you call the functions without any arguments. For now, you can just call the functions that require an argument with None, e.g. new_file(None). The None object is used to define a null value (no value at all). None is not the same as 0, False, or an empty string. None is an object type (NoneType) and only None can be None.
How will you call a function that takes two arguments?
Whenever you prompt for input be sure to allow the response to the prompt to be on the same line and to put a space after the prompt to increase readability.
Be sure to skip a line between each prompt.
Error Handling
Your code should print an error message describing the error if the user enters a number outside the range 1-7, and a different error message if the user enters anything other than an integer. Use a try-except block.
Your code should keep looping until the user enters 7 to quit.
Remember to remove the unit tests from main() that you coded lab_assignment_2, but your convert_units() function should stay because it will be used later.
A note about elegance:
One indication that your code needs more elegance is when you have the same statements repeated. For example, do you have “What is your choice” more than once in your code? Is there more than one place that you call print_menu()? If either of these is true, your code should be improved to make it more elegant. Many times this happens because you can’t figure out how to make something happen at the right place in the loop. Take the time to figure this out.
Finally, a couple of reminders about style that we haven’t discussed yet, but need to keep in mind starting now:
The indent should consistently be multiples of four spaces (no tabs)
No line should be more than 79 characters long
The only exception to this is the final multiline string containing your program’s output, paste it in exactly as it appears.
Here’s a sample run (remember, it looks like nothing is happening, but the menu is calling each function), your output must match what you see below:
STEM Center Temperature Project
Mike Murphy
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 1
New File Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 2
Choose Units Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 3
Change Filter Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 4
Summary Statistics Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 5
Print Temp by Day/Time Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 6
Print Histogram Function Called
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 9
Invalid Choice, please enter an integer between 1 and 7.
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? -1
Invalid Choice, please enter an integer between 1 and 7.
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? a
*** Please enter a number only ***
Main Menu
———
1 – Process a new data file
2 – Choose units
3 – Edit room filter
4 – Show summary statistics
5 – Show temperature by date and time
6 – Show histogram of temperatures
7 – Quit
What is your choice? 7
Thank you for using the STEM Center Temperature Project
Process finished with exit code 0