Programs That Can Compare

Learn how to choose portions of code to execute.

Project requirements

Let’s attend to our client’s requirements for the project once again, this time focusing on the requirements highlighted in green for updating the score when the user answers a maths question correctly.

Project requirements that require selective execution
Project requirements that require selective execution

What if

We know we’ll only need == to check for the answer’s correctness. What if we had something in Python that could let us update the score, but only if the user_answer == correct_answer:

Python 3.10.4
score = 0
if user_answer == correct_answer:
score = score + 1
else:
print("Unfortunately your answer is not correct.")

Now, this code reads neatly. This is as easy as it looks—Python lets us use an if-else to control the flow of code execution.

Technical Quiz
1.

(Select all that apply.) In the code given above, which line numbers will not get executed when the value of user_answer is equal to correct_answerMulti-select

A.

Line 1

B.

Line 2

C.

Line 3

D.

Line 4

E.

Line 5


1 / 2

Bringing if inside our project

Let’s code to fulfill the project requirements. Let’s introduce a variable score.

score = 0
user_answer = int(input("What is 4*7? "))
correct_answer = 4 * 7
print("Your score is", score)
Initializing a variable to store the score

Why don’t we now bring our if code structure that we designed earlier so that the score only gets updated when the user_answer == correct_answer:

score = 0
print("Your score is", score)
user_answer = int(input("What is 4*7? "))
correct_answer = 4 * 7
if user_answer == correct_answer:
    score = score + 1
print("Your new score is: ", score) 
Updating the score on the correct answer

Run it first for 28, then next time run it for 20. What’s the difference? Of course, the score will now only get updated when the correct answer is input by the user.

Indentation matters

Now, let’s make a minor indentation change. Let’s see what is going to change:

score = 0
print("Your score is", score)
user_answer = int(input("What is 4*7? "))
correct_answer = 4 * 7
if user_answer == correct_answer:
      score = score + 1
      print("Your new score is: ", score) 
Changing indentation in the code

The difference from the previous code is that now the print statement in line 7 only gets executed as part of the if block, not otherwise. Again, run it for 28 and 20. This gives us the ability to do multiple actions in case an if block is to be executed. The same is the case for the else block.

Let’s finish the requirements

Let’s write a code that does two things if the user_answer is correct, i.e., it updates the score and prints an affirmative message for the user. Or else, it does not update the score and only informs the user that their answer wasn’t correct.

score = 0
print("Your score is", score)
user_answer = int(input("What is 4*7? "))
correct_answer = 4 * 7
if user_answer == correct_answer:
      score = score + 1
      print("Due to correcly answering, your score is: ", score) 
else:
      print("Sadly, that is not a correct answer")
Providing feedback based on the score

Learn about if elif else

We now know how a simple if-else structure works. However, there are situations where we might have to create a nested if-else, within another if-else. This complexity makes the code less readable eventually. Here’s a simple example to illustrate our point. We want to greet the user appropriately, given the time of the day. Within the first 12 hours of the day, we say “good morning” otherwise, we have two possibilities: a “good afternoon” if it is less than 18:00 hours, otherwise a “good evening.” The code looks like this:

Python 3.10.4
# Using if-else
hour = 15 # Try assigning hour different values, e.g. 11, 16, 20
if hour < 12:
greeting = "Good morning"
else:
if hour < 18:
greeting = "Good afternoon"
else:
greeting = "Good evening"
print(greeting)

Python gives us a better option for what’s happening in lines 5 and 6. It gives us a keyword elif (short for else and if). The use of elif makes the code so much more readable:

Python 3.10.4
# Using elif
hour = 15
if hour < 12:
greeting = "Good morning"
elif hour < 18:
greeting = "Good afternoon"
else:
greeting = "Good evening"
print(greeting)

Notice how we used else: at the end. This else is only executed when all the previous conditions have failed (are False).

In short, if-elif-else provides a way to create a chain of conditions where each condition is checked in sequence and the corresponding block of code associated with the first true condition is executed. Whether it’s simple if statements, if-else statements, or if-elif-else statements, we use them based on what we want to check in our program.

Review of the lesson

We have added another important skill to our toolkit with the help of the if-else structure. This will come in very handy in our lifelong programming careers.