Existence checking in code

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

Existence checking in code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which condition correctly checks that a user typed nothing into a field?
A if value.strip() = '':
B if value.strip() == '':
2. A user types three spaces and presses Enter. Which check catches this as missing data?
A if value.strip() == '':
B if value == '':
3. True or false: an existence check tests whether the value is sensible, such as an age between 0 and 120.
A False - it only tests that something was entered
B True - it tests that the value is sensible
4. Which code shows a message when the name field is missing?
A if name.strip() != '': print('Please enter a name')
B if name.strip() == '': print('Please enter a name')
5. How can you check that a list has at least one item before using it?
A if len(scores) == 0:
B if scores.strip() == '':

Match the Pairs

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

Left columnRight column
1if value.strip() == '': Aif name.strip() == '': print('Name is required')
2if len(items) == 0: BA single = is assignment and causes a syntax error. A comparison needs ==, as in value.strip() == ''.
3Show an error message when a required field is left blank CExistence check: True when the input is empty or only spaces, so nothing real was entered
4Stop space-only input from passing an existence check by removing the spaces first DWithout strip(), a space-only input is not equal to '', so the existence check wrongly passes. Use value.strip() == ''.
5if value == '': print('Missing') # user types three spaces Etext = input('Enter: ').strip()
6if value.strip() = '': FExistence check on a list: True when the list holds no items, so there is no data to use

Definitions

if value.strip() == '':

Fill in the Blanks

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

1.
value = input("Enter your name: ") if value._____() == '': print("Please enter a value")
2.
name = input("Name: ") if name.strip() __ '': print("Name is required")
3.
scores = [] if ___(scores) == 0: print("No scores entered")
4.
city = input("City: ") if city.strip() == '': _____("City is missing")
5.
import os if not os.path.______("data.csv"): print("File not found")

Spot the Error

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

Question 1
A name = input('Name: ')
B if name.strip() == '':
C print('Welcome', name)
D else:
E print('Hello', name)
Question 2
A email = input('Email: ')
B if email.strip() == '':
C print('Email is required')
D else
E print('Saved')
Question 3
A age = input('Age: ')
B while age.strip() == '':
C print('Age is required')
D age = age.strip()
E print('Thanks')
Question 4
A city = input('City: ')
B if city.strip() = '':
C print('City is required')
D else:
E print(city)
Question 5
A code = print('Code: ')
B if code.strip() == '':
C print('Code missing')

Answer Key

Existence checking in code
Section A: Quick Questions
Q1
B — if value.strip() == '':
Q2
A — if value.strip() == '':
Q3
A — False - it only tests that something was entered
Q4
B — if name.strip() == '': print('Please enter a name')
Q5
A — if len(scores) == 0:
Section B: Match the Pairs
Q1
if value.strip() == '': → Existence check: True when the input is empty or only spaces, so nothing real was entered
Q2
if len(items) == 0: → Existence check on a list: True when the list holds no items, so there is no data to use
Q3
Show an error message when a required field is left blank → if name.strip() == '': print('Name is required')
Q4
Stop space-only input from passing an existence check by removing the spaces first → text = input('Enter: ').strip()
Q5
if value == '': print('Missing') # user types three spaces → Without strip(), a space-only input is not equal to '', so the existence check wrongly passes. Use value.strip() == ''.
Q6
if value.strip() = '': → A single = is assignment and causes a syntax error. A comparison needs ==, as in value.strip() == ''.
Section C: Definitions
Q1
if value.strip() == '': — An existence check tests that a value was actually entered. strip() removes spaces, so this is True for empty or space-only input, and an error message can be shown.
Section D: Fill in the Blanks
Q1
value = input("Enter your name: ") if value.strip() == '': print("Please enter a value")
Q2
name = input("Name: ") if name.strip() == '': print("Name is required")
Q3
scores = [] if len(scores) == 0: print("No scores entered")
Q4
city = input("City: ") if city.strip() == '': print("City is missing")
Q5
import os if not os.path.exists("data.csv"): print("File not found")
Section E: Spot the Error
Q1
C — This branch runs when the name is empty, so it should show a missing-data message such as print('Name is required'), not a welcome that uses the empty value.
Q2
D — The else keyword must be followed by a colon. It should be else:.
Q3
D — The loop never asks again, so age stays empty and the loop runs forever. The line should ask again with age = input('Age: ').
Q4
B — A single = is assignment, not comparison. The existence check needs == to compare with the empty string.
Q5
A — print() only displays text and does not read what the user types. The value must be read with input('Code: ').