CS170: Programming for the World Around Us - Conditionals in Python

Activity Goals

The goals of this activity are:
  1. To be able to write an if statement
  2. To be able to write an else statement
  3. To design boolean expressions for conditionals
  4. To combine the if and else blocks to form conditionals that utilize the else if construct
  5. To implement complex conditional statements using boolean expression operators

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity

Directions

Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Conditionals for Selective Execution with if Statements

Questions

  1. Try running the above program for different ages (say, 18, 34, 35, and 36).
  2. What is the purpose of the indented text below the if line? What would happen if you removed that indentation, or added another print statement below that was not indented?

Model 2: Conditionals for Selective Execution with if/else Statements

Questions

  1. Write and execute an if/else statement that determines if it is warm and not raining outside, and prints out whether or not it is appropriate to go outside.

Model 3: Creating a Waterfall of Possibilities by combining else and if


Questions

  1. Which code structure above do you prefer and why?
  2. What does it mean to put an if statement inside of the body of another if statement? Give a scenario in which each line of code will execute, and a scenario in which it will not.
  3. Can you switch the order of the if statements in either example? Why or why not?

Model 4: Compound if conditionals

Questions

  1. What text should go into the print statement to indicate whether the person can vote (at least age 18) but also is too young to run for president (at least age 35)?
  2. Can you switch the order of the checks inside the if statement? Why or why not?
  3. Consider the letter grade breakdown table on our course syllabus. Write a series of compound if statements that determines if your grade is an A+, an A, or an A-.

Model 5: Try It Out!

Questions

  1. What does the == operator do? Why do you think it is different from a single = sign?
  2. With a partner, write a short program that either tells an interactive story or treasure hunt, asks a user to guess a secret number (and tells them if they are correct), plays rock-paper-scizzors, plays a question and answer quiz game, or tells you whether you should turn on the heater or air conditioner. Be prepared to share this with the class!

Model 6: Using Flow Charts to Observe Conditional Program Flow

If-Then-Else-diagram

Questions

  1. Draw a flowchart diagram that illustrates the control flow of your Venn Diagram program.
  2. Draw a flowchart of a conditional that checks if your grade is within range for each letter grade in the class.

Model 7: Putting It All Together: Implementing a Venn Diagram

Empty 3-way Venn Diagram

Questions

  1. Make up a 3-way Venn Diagram of your choosing; you can look one up on the Internet if you wish.
  2. Label the three large circles "A", "B", and "C". In each of the 7 regions within the Venn Diagram, which elements are true and which are false?
  3. Write a series of if statements that may use else and else if blocks that print out the different states of your Venn Diagram. There are a few ways to go about this, so we will discuss and compare approaches as a class.

Model 8: Using Flow Charts to Observe Conditional Program Flow

If-Then-Else-diagram

Questions

  1. Draw a flowchart diagram that illustrates the control flow of your Venn Diagram program.
  2. Draw a flowchart of a conditional that checks if your grade is within range for each letter grade in the class.

Notes and Walkthrough

Programs That Make Decisions

Every program we’ve written so far runs the same lines, in the same order, every single time. But think about your own day: if it’s raining, you grab an umbrella; otherwise, you don’t. Real decisions depend on conditions! A conditional is a statement that lets a program choose whether to run some code based on a question with a yes-or-no answer. Each possible path the program can take is called a branch — just like a fork in a hiking trail.

Boolean Expressions: Questions with Yes/No Answers

Before we can branch, we need a way to ask a question. A boolean expression is an expression whose value is either True or False — those two values are called booleans (named after the mathematician George Boole). We build boolean expressions with comparison operators:

Operator Question it asks Example Value
== Are these equal? 5 == 5 True
!= Are these different? 5 != 5 False
< Is the left smaller? 3 < 7 True
> Is the left bigger? 3 > 7 False
<= Smaller or equal? 18 <= 18 True
>= Bigger or equal? 34 >= 35 False

Notice that checking equality uses two equals signs (==), while assignment (storing a value) uses one (=). This is probably the number one typo in all of programming, so keep an eye out! You can print a boolean expression just like anything else:

age = 21
print(age >= 18)
print(age >= 35)
print(age == 21)

This program prints:

True
False
True

The if Statement: Running Code Only Sometimes

An if statement takes a boolean expression (called the condition) and a block of code, and runs the block only when the condition is True. You’ve seen the presidential-age example in the model above — here it is again with one extra line, which turns out to be an important experiment:

age = 38

if age >= 35:
    print("You are old enough to run for President of the United States!")
print("Thanks for checking!")

This program prints:

You are old enough to run for President of the United States!
Thanks for checking!

Three things to notice. First, the condition ends with a colon (:) — that’s how Python knows the decision-making block is about to begin. Second, the line “inside” the if is indented (pushed to the right, by convention four spaces). In Python, indentation isn’t decoration — it’s how Python knows which lines belong to the if. Third, the last print is not indented, so it runs no matter what. What do you think prints if we change age to 21? Only Thanks for checking! — the indented line is skipped, but the unindented line still runs.

else: What to Do Otherwise

Often we want to do one thing when the condition is True and something different when it’s False. That’s the job of else — it has no condition of its own, because it simply catches everything the if didn’t:

age = 21

if age >= 35:
    print("You are old enough to run for President of the United States!")
else:
    print("You're too young to run for President.")

This program prints:

You're too young to run for President.

Exactly one of the two branches runs — never both, and never neither. Can you think of decisions in your life that work like this? “If the dining hall has pizza, I’ll get pizza; otherwise, I’ll make a sandwich.”

elif: A Waterfall of Possibilities

What if there are more than two possibilities? Our voting example has three: old enough to run for President, old enough only to vote, or too young for both. We could nest an if inside another if (as in the model), but Python gives us a tidier tool: elif, short for “else if.” Python checks each condition from top to bottom and runs the first branch whose condition is True — then skips all the rest, like water falling past ledges until it lands:

age = int(input("How old are you? "))

if age >= 35:
    print("You are old enough to run for President of the United States!")
elif age >= 18:
    print("You can't run for President, but you are old enough to vote!")
else:
    print("You're too young to run for President, and too young to vote.")

This program prints (if the user types 21):

How old are you? 21
You can't run for President, but you are old enough to vote!

Here’s a trace showing which branch fires for several different inputs. Being able to build a table like this in your head is what “reading” a conditional really means:

Input age age >= 35? age >= 18? Which branch runs Output
40 True (not even checked!) the if branch You are old enough to run for President…
35 True (not checked) the if branch You are old enough to run for President…
21 False True the elif branch You can’t run for President, but you are old enough to vote!
18 False True the elif branch You can’t run for President, but you are old enough to vote!
12 False False the else branch You’re too young to run for President, and too young to vote.

Notice something subtle: when age is 40, Python never even looks at the elif condition. Once one branch fires, the whole chain is done. That’s why the order of the checks matters — what would go wrong if we checked age >= 18 first? (Try it: a 40-year-old would be told they can only vote, because 40 >= 18 is True and fires first!)

Combining Conditions with and, or, and not

Sometimes one comparison isn’t enough. “You can vote but not run for President” needs two facts to be true at once. Python gives us three boolean operators for combining conditions:

  • andTrue only when both sides are True
  • orTrue when at least one side is True
  • not — flips True to False and vice versa

Let’s finish the compound conditional from the model, and add a movie-ticket example. Many theaters give a discount to children or seniors:

age = 25

if age >= 18 and age < 35:
    print("You can vote, but you can't run for President yet.")

if age <= 12 or age >= 65:
    print("You get a discounted movie ticket!")
else:
    print("You pay full price. Sorry!")

This program prints:

You can vote, but you can't run for President yet.
You pay full price. Sorry!

Why is the first condition age < 35 and not age <= 35? Because a 35-year-old can run for President — so 35 should not be part of the “can’t run yet” group. Getting these boundary values right is a big part of writing correct conditionals. Here’s a trace of the ticket conditional for a few ages:

Input age age <= 12? age >= 65? or result Which branch Output
8 True False True if You get a discounted movie ticket!
25 False False False else You pay full price. Sorry!
70 False True True if You get a discounted movie ticket!

And not? It’s great for readability: if not raining: reads almost like English. Recall the model question about going outside — here’s one way to write it:

temperature = 72
raining = False

if temperature >= 60 and not raining:
    print("It's a great day to go outside!")
else:
    print("Maybe stay in with some hot chocolate.")

This program prints:

It's a great day to go outside!

Nesting: Decisions Inside Decisions

You saw in the model that an if can live inside another if — that’s called nesting. The inner question is only ever asked when the outer answer was “yes.” Nesting is perfect when one decision only makes sense after another. For example, a theme park ride might require you to be tall enough first, and only then check whether you want the front row:

height = int(input("How tall are you (in inches)? "))

if height >= 48:
    seat = input("You can ride! Front row or back row? ")
    if seat == "front":
        print("Brave choice! Enjoy the splash zone.")
    else:
        print("Enjoy the ride from the back!")
else:
    print("Sorry, you must be 48 inches tall to ride.")

This program prints (if the user types 60 and then front):

How tall are you (in inches)? 60
You can ride! Front row or back row? front
Brave choice! Enjoy the splash zone.

Notice how the indentation shows the structure: the inner if/else is indented twice, because it lives inside the outer if’s block. Could you rewrite this with and instead of nesting? Sometimes yes — if height >= 48 and seat == "front" — but then where would you ask for the seat? Nesting lets us wait to ask the second question until we know it matters.

Common mistakes to avoid

  • Using = when you mean ==. if age = 18: is a syntax error. One = stores, two == compares.
  • Forgetting the colon at the end of if, elif, and else lines. Python will point at the line and complain about syntax.
  • Indentation slips. Everything that belongs to a branch must be indented the same amount underneath it. A line you meant to be inside the if but forgot to indent will run every time!
  • Comparing a string to a number. input() gives a string, so if input("Age? ") >= 18: fails. Convert first: age = int(input("Age? ")).
  • Conditions in the wrong order in an elif chain. Put the most specific (largest / strictest) checks first — remember the President-vs-voting example above.
  • Boundary mix-ups between < and <=. Ask yourself: should the boundary value itself (18, 35, 65…) take this branch or the other one?
  • Writing if age >= 18 and <= 34: — Python needs a complete comparison on each side of and: if age >= 18 and age <= 34:.
  • Case-sensitive string comparisons. "Left" == "left" is False! A common fix is to lowercase the input first: choice = input("Left or right? ").lower().

Practice Exercises

Try each one yourself before revealing the solution — struggling a little first is where the learning happens!

Exercise 1 (warm-up)

Ask the user for their age and print You can vote! if they are 18 or older. (Nothing needs to print otherwise — yet.)

Sample run:

How old are you? 19
You can vote!
Click to reveal a solution to Exercise 1 ```python age = int(input("How old are you? ")) # convert the string from input() to a number if age >= 18: # condition: a boolean expression print("You can vote!") # runs only when the condition is True ``` Remember the conversion with `int()` — comparing the raw string from `input()` against a number is an error.

Exercise 2 (if/else)

Extend Exercise 1: if the user is younger than 18, tell them how many years until they can vote.

Sample run:

How old are you? 15
You can vote in 3 years!
Click to reveal a solution to Exercise 2 ```python age = int(input("How old are you? ")) if age >= 18: print("You can vote!") else: years = 18 - age # arithmetic inside a branch is fine! print("You can vote in " + str(years) + " years!") ``` Exactly one branch runs. Note the `str()` when gluing the number into the message.

Exercise 3 (elif chain)

Write a movie ticket pricer: age 12 and under pays $6, age 65 and older pays $8, and everyone else pays $12. Print the price.

Sample runs:

How old are you? 10
Your ticket costs $6
How old are you? 30
Your ticket costs $12
Click to reveal a solution to Exercise 3 ```python age = int(input("How old are you? ")) if age <= 12: print("Your ticket costs $6") elif age >= 65: print("Your ticket costs $8") else: print("Your ticket costs $12") # everyone who isn't a child or senior ``` The `else` needs no condition — it catches every age that fell past the first two checks. Try tracing ages 12, 13, 64, and 65 to confirm the boundaries.

Exercise 4 (challenge: combining conditions)

A local pool has these rules: you can swim in the deep end only if you are at least 13 years old and have passed the swim test. Ask the user for their age and whether they passed the test (have them type yes or no), and print one of: Deep end is open to you!, Shallow end only — take the swim test! (right age, no test), or Shallow end only for now. (too young).

Sample run:

How old are you? 15
Did you pass the swim test (yes/no)? no
Shallow end only — take the swim test!
Click to reveal a solution to Exercise 4 ```python age = int(input("How old are you? ")) passed = input("Did you pass the swim test (yes/no)? ") if age >= 13 and passed == "yes": # both must be True print("Deep end is open to you!") elif age >= 13: # old enough, so the test must be the problem print("Shallow end only — take the swim test!") else: print("Shallow end only for now.") ``` The `elif age >= 13` branch only runs when the first condition failed — so if we got there and the age was fine, the swim test must be what was missing. We got that fact "for free" from the waterfall!

Exercise 5 (extra challenge: grades)

Using the letter grade idea from the model questions: ask for a numeric grade from 0 to 100 and print the letter grade — A for 90 and above, B for 80 to 89, C for 70 to 79, D for 60 to 69, and F below 60. Then, make sure a grade like 104 or -3 prints That's not a valid grade! instead. (Hint: check for invalid input first — why?)

Sample run:

What is your grade? 87
You earned a B
Click to reveal a solution to Exercise 5 ```python grade = int(input("What is your grade? ")) if grade < 0 or grade > 100: # catch the impossible values first print("That's not a valid grade!") elif grade >= 90: print("You earned an A") elif grade >= 80: # we only get here if grade < 90 print("You earned a B") elif grade >= 70: print("You earned a C") elif grade >= 60: print("You earned a D") else: print("You earned an F") ``` Two key ideas: the invalid check goes first so that `104` doesn't slip into the A branch, and each `elif` doesn't need an upper bound (like `grade < 90`) because the waterfall guarantees earlier conditions already failed.

Submission

I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.