Hard7 marksExtended Response
ProgrammingGeneralpythoncodingalgorithms

AQA GCSE · Question 12.2 · Programming

Figure 12 shows a line of Python code that creates a list of fruit names.

Figure 12
python
fruits = ["banana", "apple", "orange", "pear", "grape", "pineapple"]
Extend the program in Figure 12. Your answer must be written in Python.

The program should get the user to enter a word and perform a linear search on the list fruits to find if the word is in the list or not.

The program should:

  • ask the user what word they would like to find
  • output the message True if the word is found
  • output the message False if the word is not found.

You must write your own linear search routine and not use any built-in search function available in Python.

How to approach this question

1. **Get Input:** Use `input()` to get the word to search for from the user. Store it in a variable, e.g., `word_to_find`. 2. **Initialize a Flag:** Create a boolean variable, e.g., `found = False`. This variable will keep track of whether the word has been found. It starts as `False` because we haven't found it yet. 3. **Loop Through the List:** Use a `for` loop to iterate through each item in the `fruits` list. For example: `for fruit in fruits:`. 4. **Check for a Match:** Inside the loop, use an `if` statement to compare the current item (`fruit`) with the user's input (`word_to_find`). 5. **Update Flag and Exit:** If they match (`if fruit == word_to_find:`), set your flag to `True` (`found = True`) and use `break` to exit the loop, as there is no need to search further. 6. **Print Result:** After the loop has finished, print the final value of your `found` variable.

Full Answer

python fruits = ["banana", "apple", "orange", "pear", "grape", "pineapple"] # Ask the user for the word to find word_to_find = input("Enter a fruit to search for: ") # Perform a linear search found = False for fruit in fruits: if fruit == word_to_find: found = True break # Stop searching once found # Output the result print(found)
A linear search is the simplest search algorithm. It sequentially checks each element of the list until a match is found or the whole list has been searched. The Python solution implements this as follows: 1. `word_to_find = input(...)`: Gets the target word from the user. 2. `found = False`: A "flag" variable is created. Its value will be changed to `True` only if we find a match. 3. `for fruit in fruits:`: This loop iterates through every element in the `fruits` list, assigning the current element to the variable `fruit` in each pass. 4. `if fruit == word_to_find:`: Inside the loop, this checks if the current fruit is the one we are looking for. 5. `found = True`: If a match is found, we "raise the flag" by setting `found` to `True`. 6. `break`: This is an important optimization. Once the item is found, there's no need to check the rest of the list, so `break` immediately exits the loop. 7. `print(found)`: After the loop finishes (either by `break` or by checking all items), this line prints the final state of the `found` flag, which will be `True` if a match was made, and will remain `False` otherwise.

Common mistakes

✗ Using the built-in Python `in` operator (e.g., `if word_to_find in fruits:`), as the question explicitly forbids this.\n✗ Forgetting to initialize the `found` flag before the loop.\n✗ Printing inside the loop, which could result in multiple outputs.

Practice the full AQA GCSE Computer Science Paper 1 Python

31 questions · hints · full answers · grading

More questions from this exam