← WA Computer Science ATAR Unit 1

List check with the in operator

A list check uses in to accept a value only if it appears in a list of allowed values, e.g. choice in ['a', 'b']. Use not in to reject a value that is missing.

Understanding List check with the in operator in WA Computer Science ATAR Unit 1

What it does: A list check compares the input against a list of allowed values using in. The in operator tests whether a value appears in a list. A single = is assignment, not a check. 'x' is not one of the items in the list, so the in check gives False. The correct form is not in. Writing in not is a syntax error. Use lower() on the typed string, not on the list. Lists have no lower() method, so the second line would crash. input() returns a string, so the list must hold strings. The text '1' is not equal to the number 1.

Common mistakes: == compares a single string with the whole list, so it is never True. A list check needs the in operator: if choice in allowed:. The menu items are lowercase, so converting the input to uppercase means it can never match. Use .lower() for a case-insensitive check.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 100–314) and word search (Modules 100–470).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line checks that choice is one of the allowed menu options?
if choice = ['a', 'b', 'c']:if choice in ['a', 'b', 'c']:
Matches
if choice in '1', '2', '3':
↓
The allowed values are not inside a list. They need square brackets, like ['1', '2', '3'].
Applying
Meanings
Term
in
Meaning
A list check uses in to accept a value only if it appears in a list of allowed values, e.g. choice in ['a', 'b']. Use not in to reject a value that is missing.
Practice
allowed = ['a', 'b', 'c']
Testing
Gaps
if choice ["a", "b", "c"]:
Check Answer
Spots
3choice = choice.lower()
4if choice == allowed:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hermit Crab' badge — Definition Collector, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line checks that choice is one of the allowed menu options?
A. if choice = ['a', 'b', 'c']:
B. if choice in ['a', 'b', 'c']: ✓
The in operator tests whether a value appears in a list. A single = is assignment, not a check.
2. True or false: 'x' in ['a', 'b', 'c'] gives True.
A. False ✓
B. True
'x' is not one of the items in the list, so the in check gives False.
3. Which line rejects a value that is NOT in the allowed list?
A. if choice not in options: ✓
B. if choice in not options:
The correct form is not in. Writing in not is a syntax error.
4. The user types 'Y'. The allowed list is ['y', 'n']. What is the best way to accept it?
A. if choice.lower() in ['y', 'n']: ✓
B. if choice in ['Y', 'y', 'N', 'n'].lower():
Use lower() on the typed string, not on the list. Lists have no lower() method, so the second line would crash.
5. Which is a valid list check for a menu that accepts 1, 2 or 3 typed with input()?
A. if choice in [1, 2, 3]:
B. if choice in ['1', '2', '3']: ✓
input() returns a string, so the list must hold strings. The text '1' is not equal to the number 1.

Match the Pairs

choice in ['a', 'b', 'c'] → Gives True only if choice matches one of the allowed values in the list
if choice not in options: → Runs the indented block when the value is missing from the allowed list, so it can be rejected
Accept a menu choice only if it is one of the allowed options → if choice in ['1', '2', '3']:
Make the list check case-insensitive so 'Y' and 'y' are both accepted → if answer.lower() in ['y', 'n']:
if choice in '1', '2', '3': → The allowed values are not inside a list. They need square brackets, like ['1', '2', '3'].
if answer in ['y', 'n']: # user types 'Y' → The check is case-sensitive, so 'Y' is rejected. Use answer.lower() in ['y', 'n'] to accept any case.

Key Term

in
A list check uses in to accept a value only if it appears in a list of allowed values, e.g. choice in ['a', 'b']. Use not in to reject a value that is missing.

Fill in the Blanks

1. if choice in ["a", "b", "c"]:
Hint: Type the operator that checks whether a value is a member of a list.
2. if choice not in ["a", "b", "c"]:
Hint: Type the operator that is True when the value is missing from the list of allowed values.
3. if choice.lower() in ["y", "n"]:
Hint: Type the string method that makes matching case-insensitive by converting text to small letters.
4. options = ["pizza", "pasta", "salad"] meal = input("Meal: ") if meal in options:
Hint: Type the name of the variable that holds the list of allowed menu choices.
5. ok = ["red", "green", "blue"] colour = input("Colour: ") print(colour in ok)
Hint: Type the operator that gives True when the typed value appears in the list.

Spot the Error

Question 1
A allowed = ['a', 'b', 'c']
B choice = input('Pick: ')
C choice = choice.lower()
D if choice == allowed:
E print('OK')
Line D — == compares a single string with the whole list, so it is never True. A list check needs the in operator: if choice in allowed:
Question 2
A menu = ['pizza', 'pasta']
B item = input('Food: ').upper()
C if item in menu:
D print('Added')
Line B — The menu items are lowercase, so converting the input to uppercase means it can never match. Use .lower() for a case-insensitive check.
Question 3
A colours = ['red', 'blue']
B c = input('Colour? ')
C c = c.lower()
D if c not in colours:
E print('Valid colour')
Line D — The program prints 'Valid colour', so it should accept values that are in the list. not in would accept only invalid colours. It should be: if c in colours:
Question 4
A days = ['Sat', 'Sun']
B d = input('Day: ')
C if d in days[0]:
D print('Weekend')
Line C — days[0] is just the string 'Sat', so in would check for a piece of that text instead of checking the list. Use the whole list: if d in days:
Question 5
A valid = ['y', 'n']
B ans = input('Y/N? ').lower
C while ans not in valid:
D ans = input('Y/N? ')
E print('Thanks')
Line B — lower is missing its brackets, so the method is never called and ans is not converted to lowercase. It should be .lower()

Student with a class code? Join your class →