AQA Computer Science GCSE
Programming project - Menu Systems
A program menu is often a key starting point for a project. This lets the user make choices about what to do.
This is likely to involve:
- outputting a heading and perhaps a greeting
- outputting a list of options for the user to choose from
- allowing the user to enter a choice
- doing that choice
After the user's first choice has been completed the program will often return to the same menu, so a way of repeating the menu is essential. One of the options will usually allow the user to quit the program - if they pick this then the program needs to end somehow.
So, the possible choices might be:
OUTPUT "R: Resume a saved game"
OUTPUT "H: View high scores"
OUTPUT "Q: Exit the game"
The easiest way to repeat the menu is to wrap it in a WHILE loop that continues until Q is entered. This might look something like:
WHILE choice ≠ "Q"
OUTPUT "" #blank line
OUTPUT "N: Play a new game"
OUTPUT "R: Resume a saved game"
OUTPUT "H: View high scores"
OUTPUT "Q: Exit the game"
OUTPUT ""
choice <- USERINPUT
This will continue until the user enters "Q".
To do anything, the program needs to respond to the user input. The easiest way to do this is to use subroutines (in Python: functions) which get called based on the choice entered by the user. This will work along the lines of:
A series of IF statements will be needed - probably in the form of nested IFs or using ELSE–IF statements in Python.
One important thing to consider is that happens if the user enters something unexpected (say, "X"). The program can't crash, but it should also tell them that they've entered something odd. This is usually dealt with by using an ELSE statement:
ELSE
OUTPUT "Please make a choice from the list of options."
As ever, you have to make sure the IF blocks are balanced properly so that the program continues to do the right thing.
Putting the Menu System Together
The whole thing in Pseudocode might look something like:
WHILE choice ≠ "Q"
OUTPUT "" #blank line
OUTPUT "N: Play a new game"
OUTPUT "R: Resume a saved game"
OUTPUT "H: View high scores"
OUTPUT "Q: Exit the game"
OUTPUT ""
choice <- USERINPUT
IF choice = "N" THEN
OUTPUT "Please make a choice from the list of options."
This is much easier to do using ELSE IF statements than using nested IF.
Note that it's necessary to do something (anything) if Q is chosen, otherwise the ELSE message will appear if the user wants to quit.
Coding a Menu in Python
The pseudocode translates fairly directly into Python. Some hints:
- remember that in Python ≠ becomes !=
- and that you need to use == to check that something is equal
- adding .upper() to an input statement converts the string entry to UPPERCASE - which makes testing whatever the user entered much easier
- you can use triple quotes to enter a multi-line string which makes part of the menu much easier to write
- functions need to be set up at the top of the program
The only other thing to note is that you need to set up the subroutines (functions) when you write the menu and that each function must have something in it. The easiest thing to do is to add a simple print statement to say which function has been entered. This will also let you check that the program is working as expected:
# don't forget to outdent for the next function
Each function only needs to contain one line of code with a print statement and the program will compule and run properly. Without that it won't even compile.