Python pass · Exercises
Beginner

Python pass Exercises

4 exercises that build on the Python pass Statement lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: pass is a statement that does nothing; it exists because Python blocks cannot be empty, so you use it to hold the place of code you haven't written yet or don't need. By the end you can use pass as a placeholder in empty blocks, explain why a comment alone causes an IndentationError, choose between pass, ... and a comment, and recognise when except: pass hides real problems. You will practise: Declare two placeholders; Predict pass and continue; Fix the silent branch; Count the entries you skip.

0 / 4 completed

Python pass practice exercises

1. Declare two placeholders

Write code Easy

You're planning a small shop program. Declare two empty placeholders, each with pass as its body:

  1. a function named send_receipt: write def send_receipt(): and indent a pass line under it
  2. a class named Customer: write class Customer: and indent a pass line under it

The program must still run to the end. The print line is already there to prove it.

Your code
# 1. a function called send_receipt whose body is just pass
# 2. a class called Customer whose body is just pass

print("Stubs declared")

2. Predict pass and continue

Predict the output Medium

Read the code and type exactly what it will print. Decide for each number which branch runs and whether the print line is reached. Then press Check to run it and compare.

Read this code
for n in range(1, 6):
    if n == 2:
        pass
    elif n == 4:
        continue
    print(n)
print("end")

3. Fix the silent branch

Fix the bug Medium

This checkout code should print Not enough money when the balance is too low for the price. Right now it prints nothing about it, because a pass placeholder was left in the else branch. Replace the placeholder so the message is printed. The balance must stay at 50.

Your code
balance = 50
price = 80
if balance >= price:
    balance = balance - price
    print("Purchase complete")
else:
    pass            # TODO
print("Balance:", balance)

4. Count the entries you skip

Write code Hard

A form collects scores as text, and some entries are not numbers. Finish clean_scores(raw) so it returns two results together: a list scores of the whole numbers it could read, and a count skipped of the entries it could not read. The line return scores, skipped is already written.

A for text in raw: loop visits every entry. Inside it, try: runs a line that might fail and except ValueError: runs when int(text) cannot read the text, so the program doesn't crash. Don't ignore the bad entries with pass: count them, so the caller knows how much data was dropped.

Your code
def clean_scores(raw):
    scores = []
    skipped = 0
    # for each text in raw: try to turn it into a whole number with int()
    # and add it to scores; if that fails, add 1 to skipped
    return scores, skipped


print(clean_scores(["90", "abc", "75", "", "100"]))

Next lessonPython match Statement

More Python pass Statement examples · All Python exercises