That Blue Square Thing

AQA Computer Science GCSE

24 March 2020: as the position regarding GCSE exams is now a bit clearer, I've decided to stop adding updates to the revision pages.

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 incvolve:

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 "N: Play a new game"
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:

choice <- "" #set up choice first so loop is entered
WHILE choice ≠ "Q"
OUTPUT "Welcome to the Comedy Dogs Top Trumps Game"
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
ENDWHILE

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:

IF choice = "N" THEN
playNewGame() # calls the subroutine playNewGame
ENDIF

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:

#other IF statements here
ELSE
OUTPUT choice + " is not a valid command."
OUTPUT "Please make a choice from the list of options."
ENDIF

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:

choice <- "" #set up choice first so loop is entered
WHILE choice ≠ "Q"
OUTPUT "Welcome to the Comedy Dogs Top Trumps Game"
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
playNewGame() # calls the subroutine playNewGame
ELSE IF choice = "R" THEN
resumeOldGame()
ELSE IF choice = "H" THEN
showHighScores()
ELSE IF choice = "Q" THEN
OUTPUT "Program closing"
ELSE
OUTPUT choice + " is not a valid command."
OUTPUT "Please make a choice from the list of options."
ENDIF
ENDWHILE

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:

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:

def playNewGame():
print("Play new game")

# 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 rpoperly. Without that it won't even compile.