Hard6 marksExtended Response
ProgrammingGeneralpythoncodingvalidation

AQA GCSE · Question 13 · Programming

A B C 1 2 3 X

A programmer is writing a game using a 3x3 grid. A square on the grid is referred to by a letter (A, B, C) and a number (1, 2, 3).

Figure 15 shows part of a Python program that checks the grid reference entered by a player.
The grid reference is valid if:

  • there are exactly two characters
  • the first character entered is A, B or C
  • the second character entered is 1, 2 or 3.

Figure 15
python
check = False
while check == False:
square = ""
while len(square) != 2:
square = input("Enter grid reference (eg C2): ")
square = square.upper()
Extend the program from Figure 15 so it completes the other checks needed to make sure a valid grid reference is entered.

Your extended program must:

  • use the variable check
  • repeat the following steps until a valid grid reference is entered:
    • get the user to enter a grid reference
    • output an appropriate message if the grid reference is not valid.

How to approach this question

1. The existing code needs to be restructured. The outer `while check == False:` loop should handle getting input and validating it. The inner `while len(square) != 2:` is not ideal for the full validation logic. 2. Inside the main `while` loop, start by getting the user input and converting it to uppercase. 3. First, check the length. An `if len(square) == 2:` is a good start. If the length is not 2, print an error message in an `else` block. 4. Inside the `if len(square) == 2:` block, you can proceed with the other checks. 5. Extract the first and second characters: `col = square[0]` and `row = square[1]`. 6. Use a nested `if` statement to check if `col` is one of 'A', 'B', or 'C' AND `row` is one of '1', '2', or '3'. A good way to do this is `if col in ["A", "B", "C"] and row in ["1", "2", "3"]:`. 7. If both conditions are true, the input is valid. Set `check = True`. This will cause the outer `while` loop to terminate. 8. If the conditions are not met, use an `else` block to print an "Invalid grid reference" message. The `check` variable remains `False`, so the loop will repeat.

Full Answer

python check = False while check == False: square = input("Enter grid reference (eg C2): ") square = square.upper() if len(square) == 2: col = square[0] row = square[1] if col in ["A", "B", "C"] and row in ["1", "2", "3"]: check = True else: print("Invalid grid reference. Please try again.") else: print("Input must be two characters long. Please try again.") # The loop will only exit when a valid reference is entered. print(f"Valid reference entered: {square}")
This question requires extending an existing piece of code to perform full input validation. The goal is to create a loop that only terminates when the user enters a string that meets three criteria: correct length, valid first character, and valid second character. A good structure is a single `while` loop controlled by the `check` flag. Inside the loop, the program gets input and then uses a series of `if/else` statements to test the conditions. python # check is the flag that controls the loop check = False while check == False: # Get input on each iteration square = input("Enter grid reference (eg C2): ") square = square.upper() # First, check the length if len(square) == 2: # If length is OK, check the characters col = square[0] row = square[1] # Check if column and row are in the allowed sets if col in ["A", "B", "C"] and row in ["1", "2", "3"]: # All checks passed, set flag to True to exit loop check = True else: # Characters are invalid print("Invalid grid reference. Please try again.") else: # Length is invalid print("Input must be two characters long. Please try again.") This structure ensures that an error message is printed for the specific failure and the loop continues until all conditions are met simultaneously.

Common mistakes

✗ Putting the validation checks outside the main `while` loop.\n✗ Not providing an error message to the user.\n✗ Forgetting to set `check = True` when the input is valid, resulting in an infinite loop.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam