Home » Chapter 3 : Decisions
if-else Statements
On the previous page, we learned the simple if statement, which evaluates a condition and runs a block of code if the result of that condition is True. Often though we need to run one block of code based on a True result and a different block of code based on a False result. The if-else statement handles this scenario.
The general form of the if-else statement looks like this:
if condition:
code_block_based_on_true_condition
else:
code_block_based_on_false_condition
Notice the indentation. The lines of code in the code blocks for both the True and the False conditions are indented so the interpreter knows what lines of code are included in those blocks.
The flowchart shown here depicts the general form of the if-else statement as well. The execution flow represents the flow of the program up to the point of the if-else condition:. When the if condition is evaluated by the interpreter, the result will be either True or False. If the result is True then the execution flow moves into the code_block_based_on_true_condition and that code runs. If the condition result is False then execution flow moves into the code_block_based_on_false_condition and that code runs. When either is finished flow continues to the next statement after the indented code blocks.
Here's a code example of the if-else statement
freezing = 32
print("Program started.")
current_temp = int(input("Enter the current temperature: "))
if current_temp > freezing:
print("The current temperature is above freezing.")
else:
print("The current temperature is freezing.")
print("Program complete.")
Running the program looks like this:
Enter the current temperature: 55
The current temperature is above freezing.
For these problems, you may need to refer to the Comparison Operators and Logical Operators tables on the previous if statement page. You may also need the table operators on the Basic Operators page as well. These tables are also available in the Reference Materials.
if-else Statement: Write a Python program that prompts the user for an integer and then prints whether that number is even or odd. Hint: To determine if an integer is even or odd we can divide by 2. If the remainder is 0, the integer is even, if the remainder is 1, the integer is odd. We can use the Python modulus arithmetic operator to help us with this calculation.
Please enter an integer: 157
Your integer is odd.
if-else Statement: Write a Python program that enforces a string length requirement. This is common in circumstances like asking a new user to create a password. One of the criteria of passwords is often a minimum number of characters. Using the Python features we know so far, we can accept a string from a user, check the length of that string, and then use an if-else statement to confirm the string length meets the length requirements. For the sake of this example, let's say our minimum number of characters is 8.
Please enter a new Password of 8 Characters or More: cheese
Your proposed password of 'cheese' is too short, it must be 8 characters or more.