What are Nested if-else
statements in Python?
Nested if-else
statements in Python are if-else
statements placed inside another if-else
statement. This allows you to check multiple conditions in a structured way.
Syntax
if condition1:
# Code to execute if condition1 is true
if condition2:
# Code to execute if condition2 is true
else:
# Code to execute if condition2 is false
else:
# Code to execute if condition1 is false
How It Works:
- First, Check a Condition:
- You start with an if statement to check a condition.
- If It's True, Check Another Condition:
- If the first condition is true, you can have another if statement inside to check another condition.
- If the First Condition is False:
- You can use else to do something different if the first condition is not true.
Example:
Problem: Write a program that checks a person's eligibility to participate in a sports event based on their age and whether they have a parent’s permission. The eligibility criteria are:
- Age 18 or above: Eligible to participate without permission.
- Age 13 to 17: Eligible to participate only with parental permission.
- Age below 13: Not eligible to participate.
Solution
# Step 1: Get input from the user
age = int(input("Enter your age: "))
permission = input("Do you have parental permission? (yes/no): ")
# Step 2: Determine eligibility using nested if-else statements
if age >= 18:
print("You are eligible to participate in the sports event!")
else:
if age >= 13: # Check for ages 13 to 17
if permission == "yes": # Check for parental permission
print("You are eligible to participate in the sports event!")
else:
print("You need parental permission to participate.")
else:
print("You are not eligible to participate in the sports event.")
Output:
Enter your age: 14
Do you have parental permission? (yes/no): no
You need parental permission to participate.
Enter your age: 80
Do you have parental permission? (yes/no): yes
You are eligible to participate in the sports event!
Enter your age: 12
Do you have parental permission? (yes/no): yes
You are not eligible to participate in the sports event.
How to put two conditions in an if statement?
In Python, you can use logical operators to combine two conditions in an if statement. The most common logical operators are:
- and: Returns True if both conditions are true.
- or: Returns True if at least one of the conditions is true.
- not: Reverses the truth value of a condition.
Syntax for Using Two Conditions
if condition1 and condition2:
# Code to execute if both conditions are true
elif condition1 or condition2:
# Code to execute if at least one condition is true
else:
# Code to execute if both conditions are false
1) Using and :
hen you use the and
operator in an if
statement, both conditions must be true for the code inside the if
block to execute. If either condition is false, the code inside the if
block will not run.
Problem: Check if a person is eligible to vote based on age and citizenship status.
age = int(input("Enter your age: "))
is_citizen = input("Are you a citizen? (yes/no): ")
if age >= 18 and is_citizen == "yes":
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output:
Enter your age: 10
Are you a citizen? (yes/no): no
You are not eligible to vote.
Enter your age: 20
Are you a citizen? (yes/no): no
You are not eligible to vote.
2) Using or :
When you use the or
operator in an if
statement, only one of the conditions needs to be true for the code inside the if
block to execute. If at least one condition is true, the code will run.
Problem: Check if a person can participate in an event based on age or membership status.
age = int(input("Enter your age: "))
is_member = input("Are you a member? (yes/no): ")
if age <= 12 or is_member == "yes":
print("You can participate in the event.")
else:
print("You cannot participate in the event.")
Output:
Enter your age: 8
Are you a member? (yes/no): no
You can participate in the event.
Tags:
Leave a comment
You must be logged in to post a comment.