Python Decision Making with If, Elif, and Else
This tutorial will guide you through the basics of decision-making in Python using the `if`, `elif`, and `else` statements. These statements allow your program to execute different blocks of code based on specific conditions. They’re fundamental to creating interactive and dynamic programs.
Understanding the Structure
The basic structure of an `if` statement is as follows:
if condition:
# Code to execute if the condition is true
The `condition` is an expression that evaluates to either `True` or `False`. If the condition is `True`, the code block indented beneath the `if` statement will be executed. If the condition is `False`, the code block is skipped.
You can add `elif` (else if) statements to check additional conditions. An `elif` statement is only evaluated if the preceding `if` or `elif` conditions were `False`. Finally, the `else` statement provides a code block to execute if none of the preceding conditions were `True`.
if condition1:
# Code to execute if condition1 is true
elif condition2:
# Code to execute if condition1 is false and condition2 is true
else:
# Code to execute if condition1 and condition2 are false
Example 1: Simple Number Checker
Let’s create a program that checks if a number is positive, negative, or zero.
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
Explanation:
- We first get input from the user and convert it to an integer using `int()`. The input is stored in the variable `number`.
- The `if number > 0:` statement checks if the number is greater than 0. If it is, the message “The number is positive.” is printed.
- If the first condition is false, the `elif number < 0:` statement checks if the number is less than 0. If it is, the message "The number is negative." is printed.
- If both the `if` and `elif` conditions are false (meaning the number is neither greater than nor less than 0), the `else` statement is executed, printing “The number is zero.”.
Common Mistake: Forgetting to convert the input to an integer using `int()`. If you don’t, the program will throw a `TypeError` because the `input()` function returns a string.
# Example Input:
# Enter a number: 5
# Output:
# The number is positive.
# Example Input:
# Enter a number: -3
# Output:
# The number is negative.
# Example Input:
# Enter a number: 0
# Output:
# The number is zero.
Example 2: Calculating the Area of a Triangle
Now, let’s create a program to calculate the area of a triangle, given its base and height. We’ll use the formula: Area = (1/2) base height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = (1/2) base height
if area > 0:
print("The area of the triangle is:", area)
else:
print("Invalid input. Base and height must be positive values.")
Explanation:
- We take the base and height as input from the user using `float()`, allowing for decimal values.
- We calculate the area using the formula.
- The `if area > 0:` statement checks if the calculated area is positive. This is important because a negative area doesn’t make sense in this context.
- If the area is positive, it prints the calculated area.
- If the area is not positive (meaning either the base or height was invalid), it prints an error message.
Calculation: Let’s say the user enters base = 10 and height = 5. Area = (1/2) 10 5 = 25.0
# Example Input:
# Enter the base of the triangle: 10
# Enter the height of the triangle: 5
# Output:
# The area of the triangle is: 25.0
# Example Input:
# Enter the base of the triangle: -5
# Enter the height of the triangle: 8
# Output:
# Invalid input. Base and height must be positive values.
Example 3: Grade Calculator
This program determines a letter grade based on a numerical score. We’ll use the following scale: 90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, and below 60 = F.
score = int(input("Enter the score: "))
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print("The grade is:", grade)
Explanation:
- We get the score as input from the user.
- We use a series of `if` and `elif` statements to determine the grade based on the score range.
- The `else` statement handles the case where the score is below 60.
- Finally, we print the determined grade.
Calculation: If the score is 85, the code will execute the `elif score >= 80:` block, setting `grade` to “B”.
# Example Input:
# Enter the score: 85
# Output:
# The grade is: B
# Example Input:
# Enter the score: 55
# Output:
# The grade is: F



Leave a Reply