List check with the in operator

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

List check with the in operator

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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']:
2. True or false: 'x' in ['a', 'b', 'c'] gives True.
A False
B True
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:
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():
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']:

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

in

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if choice __ ["a", "b", "c"]:
2.
if choice ______ ["a", "b", "c"]:
3.
if choice._____() in ["y", "n"]:
4.
options = ["pizza", "pasta", "salad"] meal = input("Meal: ") if meal in _______:
5.
ok = ["red", "green", "blue"] colour = input("Colour: ") print(colour __ ok)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A allowed = ['a', 'b', 'c']
B choice = input('Pick: ')
C choice = choice.lower()
D if choice == allowed:
E print('OK')
Question 2
A menu = ['pizza', 'pasta']
B item = input('Food: ').upper()
C if item in menu:
D print('Added')
Question 3
A colours = ['red', 'blue']
B c = input('Colour? ')
C c = c.lower()
D if c not in colours:
E print('Valid colour')
Question 4
A days = ['Sat', 'Sun']
B d = input('Day: ')
C if d in days[0]:
D print('Weekend')
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')

Answer Key

List check with the in operator
Section A: Quick Questions
Q1
B — if choice in ['a', 'b', 'c']:
Q2
A — False
Q3
A — if choice not in options:
Q4
A — if choice.lower() in ['y', 'n']:
Q5
B — if choice in ['1', '2', '3']:
Section B: Match the Pairs
Q1
choice in ['a', 'b', 'c'] → Gives True only if choice matches one of the allowed values in the list
Q2
if choice not in options: → Runs the indented block when the value is missing from the allowed list, so it can be rejected
Q3
Accept a menu choice only if it is one of the allowed options → if choice in ['1', '2', '3']:
Q4
Make the list check case-insensitive so 'Y' and 'y' are both accepted → if answer.lower() in ['y', 'n']:
Q5
if choice in '1', '2', '3': → The allowed values are not inside a list. They need square brackets, like ['1', '2', '3'].
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
if choice in ["a", "b", "c"]:
Q2
if choice not in ["a", "b", "c"]:
Q3
if choice.lower() in ["y", "n"]:
Q4
options = ["pizza", "pasta", "salad"] meal = input("Meal: ") if meal in options:
Q5
ok = ["red", "green", "blue"] colour = input("Colour: ") print(colour in ok)
Section E: Spot the Error
Q1
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:
Q2
B — The menu items are lowercase, so converting the input to uppercase means it can never match. Use .lower() for a case-insensitive check.
Q3
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:
Q4
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:
Q5
B — lower is missing its brackets, so the method is never called and ans is not converted to lowercase. It should be .lower()