Let's jump into something different now. How to allow our programs to make decisions, or change their flow based on different input.
Open lab4.py from Project 2 - Temp Converter
As a first example, how might a program check if a user's password is correct?
Here we introduce the if statement along with the comparison operator ==
Essentially this reads: If the password equals "hunter2" then print out "Correct!"
But it doesn't provide the user with any feedback on an incorrect password. Nothing happens at all because the print statement will only run if the condition password == "hunter2" is true. What do we do about if it is false? Here we introduce the else statement.
A note about the equals signs:
= and == are different.
We use = to assign a value to a variable. Hence it is called the assignment operator.
We use == to compare two values. Hence it is called the comparison operator.
In the examples above you might have noticed that the print statements were indented across (two spaces).
This is an important concept in Python. Have a look at what happens below when we don't use indentation correctly.
We want to print out "Welcome to the secure area." on a new line if the password is correct. However, it also prints out if it is incorrect.
This is because it is inline with the main program and not the if statement. Unlike many other programming languages that use braces {} to define code blocks, Python uses indentation to indicate which lines of code belong together in the same block.
In Python, there are several comparison operators to evaluate the relationship between two values:
== checks if two values are equal.
!= checks if two values are not equal.
< checks if the value on the left is less than the value on the right.
> checks if the value on the left is greater than the value on the right.
<= checks if the value on the left is less than or equal to the value on the right.
>= checks if the value on the left is greater than or equal to the value on the right.
Notice in the above example the numbers you enter will satisfy multiple conditions. How could we then do something more complex then, like classify grades as 'High Distinction', 'Distinction', 'Credit', 'Pass', and 'Fail' based on numerical scores?
Here we introduce the elif statement as a way to handle multiple conditions. The else statement still catches anything 'else'.
One of the important concepts here is that only the first condition that satifies the if-elif-else chain will execute. Once a condition is satisfied, the remaining conditions in the chain are skipped. Ordering your conditions properly is crucial for your program to function as intended.
Let's have a look at a common mistake that occurs. Try entering a high score such as 90 which should receive a 'Distinction'.
Often we may need to evaluate more than one condition to make a decision. For instance, what if we want to check whether a user's age is within a particular range? Here we introduce logical operators that allow us to combine multiple conditions.
and: Checks if both conditions are True.
or: Checks if at least one condition is True.
not: Negates the condition following it.
Say you want to verify whether the user's age is between 18 and 65.
You would need to check two conditions: the age should be greater than or equal to 18 and less than or equal to 65. Here, the and operator is helpful.
Sometimes only one of the conditions needs to be true. Let's say we want to check if a person qualifies for discount if they are a student or under 18.
Here, we use the or operator.
Try some different combinations of inputs to test it out.
Complete the following requirements:
Ask the user for an integer and store it in a variable 'num'
Use an if statement to check if the number is positive, negative or zero
Ask the user for their age and store it in a variable 'age'
Use an if statement to check if the user is an adult (18+) or a minor
Ask the user for two integers, 'a' and 'b'
Use an if statement to check if both numbers are positive or both are negative.
Otherwise, print "One number is positive and the other is negative."
Press Ctrl + F5 to run the program or open a terminal and type python lab4.py
It should run without errors and you should see something like on the right:
Example:
Please enter an integer: -7
The number is negative.
Please enter your age: 19
You are an adult.
Enter the first integer (a): -3
Enter the second integer (b): 5
One number is positive and the other is negative.
You should now know how work with conditional statements to make decisions and control the flow of your code.
Let's wrap up this lab by pushing our code to GitHub:
Enter a commit message. E.g. "Lab 4 complete"
Press Commit to main
Press Push origin
Here is a video from Harvard's CS50P course that covers the concept of conditionals in much more detail. Optional, but highly recommended!