Search⌘ K
AI Features

Programs That Can Compare

Explore how to apply conditional statements like if, else, and elif in Python to control program flow. Learn to update variables such as scores based on user responses and improve code readability by using these structures.

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

...