Existence checking in Python

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

Existence checking in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line is an existence check on the variable name?
A if name != "":
B if name == 5:
2. A user presses Enter without typing anything at input(). What does the variable hold?
A An empty string ""
B The number 0
3. Which check does the same job as name != ""?
A len(name) < 0
B len(name) > 0
4. What does an existence check test?
A That the input is a whole number
B That the user entered something
5. Which loop keeps re-asking until the user enters a name?
A while name == "":
B while name != "":

Match the Pairs

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

Left columnRight column
1if name != "": Aname = input("Name: ") while name == "": name = input("Name: ")
2if len(name) > 0: BA single = assigns a value instead of comparing. Use == to test for an empty string, or != "" to check the user entered something.
3An empty string means the user pressed Enter without typing anything, so the existence check fails CChecks that the input has at least one character, so it passes the existence check
4To re-ask until something is entered, loop while the input is still empty DA length can never be less than 0, so this check never works. It should be len(name) > 0 to confirm something was entered.
5if name = "": EChecks that the user typed something, because the input is not an empty string
6if len(name) < 0: Fname = input("Name: ") if name == "": print("Required")

Definitions

if name != ""

Fill in the Blanks

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

1.
name = input("Enter your name: ") if name __ "": print("Thanks,", name)
2.
name = input("Enter your name: ") if ___(name) > 0: print("Hello", name)
3.
name = input("Name: ") while name == "": name = _____("Name: ")
4.
name = input("Enter name: ") while len(name) __ 0: name = input("Enter name: ")
5.
name = input("Enter name: ") _____ name == "": name = input("Enter name: ")

Spot the Error

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

Question 1
A name = input("Name: ")
B while name == "":
C print("Required")
D name == input("Name: ")
E print("Hi", name)
Question 2
A name = input("Name: ")
B if name == "":
C print("Hello", name)
D else:
E print("Name is required")
Question 3
A city = input("City: ")
B if len(city) < 0:
C print("Thanks")
D else:
E print("Please enter a city")
Question 4
A email = input("Email: ")
B while len(email) == 0:
C print("Email required")
D email = print("Email: ")
E print("Saved", email)
Question 5
A age = input("Age: ")
B if age == "":
C print("Age is required"
D else:
E print("Age saved")

Answer Key

Existence checking in Python
Section A: Quick Questions
Q1
A — if name != "":
Q2
A — An empty string ""
Q3
B — len(name) > 0
Q4
B — That the user entered something
Q5
A — while name == "":
Section B: Match the Pairs
Q1
if name != "": → Checks that the user typed something, because the input is not an empty string
Q2
if len(name) > 0: → Checks that the input has at least one character, so it passes the existence check
Q3
An empty string means the user pressed Enter without typing anything, so the existence check fails → name = input("Name: ") if name == "": print("Required")
Q4
To re-ask until something is entered, loop while the input is still empty → name = input("Name: ") while name == "": name = input("Name: ")
Q5
if name = "": → A single = assigns a value instead of comparing. Use == to test for an empty string, or != "" to check the user entered something.
Q6
if len(name) < 0: → A length can never be less than 0, so this check never works. It should be len(name) > 0 to confirm something was entered.
Section C: Definitions
Q1
if name != "" — An existence check tests that the user actually entered something, so the input is not an empty string. len(name) > 0 does the same job. Loop and re-ask until it passes.
Section D: Fill in the Blanks
Q1
name = input("Enter your name: ") if name != "": print("Thanks,", name)
Q2
name = input("Enter your name: ") if len(name) > 0: print("Hello", name)
Q3
name = input("Name: ") while name == "": name = input("Name: ")
Q4
name = input("Enter name: ") while len(name) == 0: name = input("Enter name: ")
Q5
name = input("Enter name: ") while name == "": name = input("Enter name: ")
Section E: Spot the Error
Q1
D — == compares instead of assigning, so name never changes and the loop never ends. It should be name = input("Name: ") to re-ask and store the new answer.
Q2
B — The check is reversed. This greets the user only when nothing was entered. An existence check should use != "" so the greeting runs only when something was entered.
Q3
B — A length can never be less than 0, so this is never true and the else branch always runs. The existence check should be len(city) > 0.
Q4
D — print() only displays text and returns None, so it never asks the user again. It should be email = input("Email: ") to re-ask until something is entered.
Q5
C — The closing bracket is missing, which causes a syntax error. It should be print("Age is required").