Welcome to the topic on Nested Conditionals! This lesson will provide you with a deep understanding of how to structure complex decision-making processes in your Python programs. By the end of this tutorial, you'll be able to use nested conditionals to create more robust and efficient code.
In programming, nested conditionals are when one or multiple if-else statements are contained within another if-else statement. This allows for more complex decision trees to be constructed in your programs.
Here's a simple example:
day = input("Enter a day of the week: ")
if day == "Monday":
print("Today is Monday.")
if day == "Holiday":
print("It's a bank holiday!")
else:
print("You have to work today.")
else:
print(f"Sorry, '{day}' is not a valid day of the week.")
In this example, we first ask for user input regarding the day of the week. If the entered value is "Monday," we then check if it's a holiday or not by using another if-else
statement nested within the first one.
Let's look at a real-world example:
def check_eligibility(age, student_status):
if age >= 18:
if student_status == "Undergraduate":
return "You are eligible to vote."
elif student_status == "Graduate":
return "You are eligible to vote."
else:
return "Invalid student status."
else:
return "You are not old enough to vote."
print(check_eligibility(20, "Undergraduate")) # Output: You are eligible to vote.
In this example, we have a function called check_eligibility
that takes two arguments - age and student status. The function checks if the individual is of voting age (18 or older) and if they're either an undergraduate or graduate student. If both conditions are met, it returns "You are eligible to vote."
What causes it:
day = input("Enter a day of the week: ")
if day == "Monday":
print("Today is Monday.")
if day == "Holiday":
print("It's a bank holiday!")
else:
print(f"Sorry, '{day}' is not a valid day of the week.")
Error message:
File "example.py", line 3
if day == "Holiday":
^
IndentationError: expected an indented block
Solution:
Fix the indentation by moving the second if
statement under the first one:
day = input("Enter a day of the week: ")
if day == "Monday":
print("Today is Monday.")
if day == "Holiday":
print("It's a bank holiday!")
else:
print(f"Sorry, '{day}' is not a valid day of the week.")
Why it happens: Python requires consistent indentation to understand which lines belong together. Proper indentation is crucial for writing readable and functional code.
What causes it:
if day == "Monday":
print("Today is Monday.")
else if day == "Tuesday":
print("Today is Tuesday.")
Error message:
File "example.py", line 2
else if day == "Tuesday":
^
SyntaxError: invalid syntax
Solution: In Python, you should use elif
instead of else if
.
if day == "Monday":
print("Today is Monday.")
elif day == "Tuesday":
print("Today is Tuesday.")
Why it happens: Python uses a different syntax than some other programming languages, and it doesn't support the else if
construct. Instead, you should use elif
.
Now that you've learned about nested conditionals, you can tackle more complex programming challenges with confidence! As you continue your journey in Python, explore other topics like loops, functions, classes, and data structures to further expand your skills. Happy coding!