🐍 Python for Everyone

← Back to exercises

Chapter 4 Practice Bank 35-45 min, hands-on

Practice Bank: Control Flow

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Still uses only what Chapters 1-4 covered: print(), variables, input(), the conversion functions, arithmetic / relational / logical / bitwise operators, is / in, and if / if-else / elif chains / nested if.

How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: Simple if

1 Above freezing?

Find # TODO 1.1. Print a message only if temperature is above 32, then add a print() right after the if block that always runs, to show the program continues either way.

2 Looks like an email

Find # TODO 1.2. Using in, print a message only if "@" is found inside an email string.

3 Odd check with bitwise &

Find # TODO 1.3. Using number & 1 == 1, print a message only if the number is odd.

4 Checking identity with is

Find # TODO 1.4. Using is (not ==), check whether a boolean variable is True and print a welcome message if so.

5 Password check

Find # TODO 1.5. Ask for a password with input() and print "Access granted." only if it matches.

💼 Scenario

Free shipping threshold

Find # TODO 1.A. If cart_total is 75 or more, print a message telling the shopper they qualify for free shipping.

💼 Interview Prep

Prove code after if always runs

Find # TODO 1.B. An interviewer asks whether code right after an if block runs even when the condition is False. Prove it, and add a comment explaining why.

Topic 2: if-else

1 Which price is higher?

Find # TODO 2.1. Compare price_a and price_b and print which is more expensive using if-else.

2 Valid percentage?

Find # TODO 2.2. Using if-else and and (or a chained comparison), print whether completion_percent is a valid percentage (0-100).

3 Vowel or consonant

Find # TODO 2.3. Using if-else and in, print whether a letter is a vowel or a consonant.

4 Odd or even, bitwise

Find # TODO 2.4. Using if-else and bitwise &, print whether value is odd or even.

5 Newsletter signup

Find # TODO 2.5. Ask "Subscribe to our newsletter? (yes/no):" with input() and print a matching if-else response.

💼 Scenario

Age-based ticket pricing

Find # TODO 2.A. Print the child or adult ticket price based on visitor_age using if-else.

💼 Interview Prep

Login permission check

Find # TODO 2.B. Using if-else and and, grant access only if the entered username and password both match the stored values.

Topic 3: elif chains

1 Traffic light action

Find # TODO 3.1. Using an elif chain, turn a traffic light color into "Stop" / "Slow down" / "Go" / "Unknown signal".

2 Positive, negative, or zero

Find # TODO 3.2. Using if/elif/else, classify a number as positive, negative, or zero.

3 Debug the Code

Find # TODO 3.3. Three separate if statements overwrite each other instead of using elif, so a 95-degree temperature gets miscategorized as "warm" instead of "hot." Find the bug and fix it.

4 FizzBuzz, one number

Find # TODO 3.4. Using an elif chain (divisible-by-15 checked first), print "FizzBuzz" / "Fizz" / "Buzz" / the number itself for a single value n.

5 Day number to weekday

Find # TODO 3.5. Ask for a day number (1-7) with input() and use an elif chain to print the matching weekday name.

💼 Scenario

Discount tier by purchase amount

Find # TODO 3.A. Using an elif chain ordered highest-threshold-first, print a Platinum/Gold/Silver/no-discount tier based on purchase_amount.

💼 Interview Prep

Why elif order matters

Find # TODO 3.B. Build a grade elif chain (highest threshold first) and add a comment explaining why checking the highest threshold first is required.

Topic 4: Nested if

1 Positive and even/odd

Find # TODO 4.1. Using a nested if (outer: positive check, inner: even/odd check), classify a number.

2 Login flow, three outcomes

Find # TODO 4.2. Using a nested if (outer: username check, inner: password check), print "Login successful.", "Incorrect password.", or "Username not found."

3 Pass, not-eligible, or fail

Find # TODO 4.3. Using a nested if (outer: passing score, inner: attendance), decide between pass / not-eligible / fail.

4 Large or small positive

Find # TODO 4.4. Using a nested if (outer: positive check, inner: magnitude check), classify a number as large/small positive, or not positive.

5 Age and ID check

Find # TODO 4.5. Ask for age with input(); if 18+, ask (with a nested input()) whether the visitor has a valid ID, to decide entry.

💼 Scenario

Bank withdrawal

Find # TODO 4.A. Using a nested if (outer: sufficient funds, inner: valid amount), process a withdrawal and print the new balance.

💼 Interview Prep

Nested if vs. a single and

Find # TODO 4.B. Write the nested-if and single-and-condition versions of the same check side by side, and explain in a comment when nesting is genuinely required.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Interview Questions →