nested if-else in python exercises

Bill 169 Published: 08/26/2024

nested if-else in python exercises

I'd be happy to help you with Python exercises on nested if-else statements! Here are a few examples:

Example 1: Simple Nested If-Else

In this example, we'll create a function that checks if an input number is even or odd, and then further checks if it's positive or negative.

def check_number(num):

if num % 2 == 0:

if num > 0:

print("The number is even and positive.")

else:

print("The number is even and negative.")

else:

if num > 0:

print("The number is odd and positive.")

else:

print("The number is odd and negative.")

Test the function

check_number(4) # Output: The number is even and positive.

check_number(-2) # Output: The number is even and negative.

check_number(3) # Output: The number is odd and positive.

check_number(-1) # Output: The number is odd and negative.

Example 2: Nested If-Else with Multiple Conditions

In this example, we'll create a function that checks if an input person meets certain conditions to be eligible for a scholarship. The conditions are:

The person's GPA must be at least 3.0 The person's SAT score must be at least 1200 The person's community service hours must be at least 100
def check_scholarship(gpa, sat, hrs):

if gpa >= 3.0:

if sat >= 1200:

if hrs >= 100:

print("The person is eligible for the scholarship.")

else:

print("The person is not eligible because of insufficient community service hours.")

else:

print("The person is not eligible because of low SAT score.")

else:

print("The person is not eligible because of low GPA.")

Test the function

check_scholarship(3.5, 1300, 120) # Output: The person is eligible for the scholarship.

check_scholarship(2.9, 1100, 100) # Output: The person is not eligible because of low GPA.

check_scholarship(3.1, 1250, 80) # Output: The person is not eligible because of insufficient community service hours.

Example 3: Nested If-Else with Logical Operators

In this example, we'll create a function that checks if an input number falls within a certain range based on two conditions. The conditions are:

The number must be between 0 and 100 The number must not be divisible by 7 or 11
def check_range(num):

if 0 <= num <= 100:

if num % 7 != 0 and num % 11 != 0:

print("The number falls within the range and is not divisible by 7 or 11.")

else:

print("The number does not fall within the range because it's divisible by 7 or 11.")

else:

print("The number does not fall within the range because it's outside the range of 0 to 100.")

Test the function

check_range(50) # Output: The number falls within the range and is not divisible by 7 or 11.

check_range(-1) # Output: The number does not fall within the range because it's outside the range of 0 to 100.

These are just a few examples of how nested if-else statements can be used in Python. You can experiment with different conditions and logical operators to create more complex logic!

Can we use else after Elif in Python?

I'd be happy to help!

In Python, the else clause is used with a loop (like for or while) and not with an if statement. The elif statement is used to check another condition if the initial condition in the if statement is false.

The syntax for using else with a loop is as follows:

for variable in iterable:
do something

else:

execute this block when the loop finishes without finding a break or continue

For example, consider a program that iterates over a list and prints each element. If the list is empty, it will print "List is empty" instead of printing each element:

fruits = []

for fruit in fruits:

print(fruit)

else:

print("List is empty")

In this case, since the fruits list is empty, the program will print "List is empty".

On the other hand, if you're trying to use an else clause with an if statement, you would get a SyntaxError. Python does not support using an else clause directly after an if or elif statement.

To achieve similar behavior to what you might expect from an else clause with an if statement, you could use the following syntax:

if condition:
do something

else:

execute this block if the condition is false

However, be aware that this syntax does not imply that the code within the else clause will only be executed when the if condition is false. It means that this code will be executed if none of the conditions in the preceding if-elif statements are true.

So, to summarize: you cannot directly use an else clause with an if or elif statement in Python. Instead, you should use a separate else clause with a loop (like for or while) if that's what you intend to achieve.