← VCE Data Analytics U4O1

Existence checking in Python

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.

Understanding Existence checking in Python in VCE Data Analytics U4O1

What it does: Pressing Enter gives input() an empty string (""). Testing name == "" catches this case so an error message can be shown. The while loop keeps running as long as name is empty. Each pass asks again, so the loop only ends once the user types something. Checking for a whole number is a type check. An existence check only tests that the input is not empty. An if statement needs a colon at the end of the line. Without it Python gives a SyntaxError. A good error message tells the user what went wrong. Here the problem is that nothing was entered, not that a number was out of range.

Common mistakes: == 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. 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.

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 63–83) and word search (Modules 63–105).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line is an existence check on the variable name?
if name == 5:if name != "":
Matches
if name = "":
↓
A single = assigns a value instead of comparing. Use == to test for an empty string, or != "" to check the user entered something.
Applying
Meanings
Term
if name != ""
Meaning
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.
Practice
name = input("Enter your name: ")
Testing
Gaps
if name "":
Check Answer
Spots
3 print("Required")
4 name == input("Name: ")

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

The 'Scorpion' badge — Venom Debugger, 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 is an existence check on the variable name?
A. if name != "": ✓
B. if name == 5:
An existence check tests that something was entered. name != "" is True when the user typed at least one character.
2. A user presses Enter without typing anything at input(). What does the variable hold?
A. An empty string "" ✓
B. The number 0
Pressing Enter with no text gives an empty string. That is exactly what an existence check looks for.
3. Which check does the same job as name != ""?
A. len(name) < 0
B. len(name) > 0 ✓
An empty string has length 0, so len(name) > 0 means something was entered. A length can never be below 0.
4. What does an existence check test?
A. That the input is a whole number
B. That the user entered something ✓
Checking for a whole number is a type check. An existence check only tests that the input is not empty.
5. Which loop keeps re-asking until the user enters a name?
A. while name == "": ✓
B. while name != "":
The loop must repeat while the name is still empty. Using != would stop the loop as soon as the input is empty.

Match the Pairs

if name != "": → Checks that the user typed something, because the input is not an empty string
if len(name) > 0: → Checks that the input has at least one character, so it passes the existence check
An empty string means the user pressed Enter without typing anything, so the existence check fails → name = input("Name: ") if name == "": print("Required")
To re-ask until something is entered, loop while the input is still empty → name = input("Name: ") while name == "": name = input("Name: ")
if name = "": → A single = assigns a value instead of comparing. Use == to test for an empty string, or != "" to check the user entered something.
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.

Key Term

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.

Fill in the Blanks

1. name = input("Enter your name: ") if name != "": print("Thanks,", name)
Hint: Type the comparison operator that means 'is not equal to', so the check passes when something was entered.
2. name = input("Enter your name: ") if len(name) > 0: print("Hello", name)
Hint: Type the built-in function that counts how many characters are in the input.
3. name = input("Name: ") while name == "": name = input("Name: ")
Hint: Type the built-in function that asks the user to type again inside the loop.
4. name = input("Enter name: ") while len(name) == 0: name = input("Enter name: ")
Hint: Type the operator that tests whether the length is equal to zero, meaning nothing was entered.
5. name = input("Enter name: ") while name == "": name = input("Enter name: ")
Hint: Type the keyword that keeps repeating the question until the input is no longer empty.

Spot the Error

Question 1
A name = input("Name: ")
B while name == "":
C print("Required")
D name == input("Name: ")
E print("Hi", name)
Line 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.
Question 2
A name = input("Name: ")
B if name == "":
C print("Hello", name)
D else:
E print("Name is required")
Line 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.
Question 3
A city = input("City: ")
B if len(city) < 0:
C print("Thanks")
D else:
E print("Please enter a city")
Line 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.
Question 4
A email = input("Email: ")
B while len(email) == 0:
C print("Email required")
D email = print("Email: ")
E print("Saved", email)
Line 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.
Question 5
A age = input("Age: ")
B if age == "":
C print("Age is required"
D else:
E print("Age saved")
Line C — The closing bracket is missing, which causes a syntax error. It should be print("Age is required").

Student with a class code? Join your class →