The conditionals can be nested within another. For example you can have if-elif-esle block inside the if / elif /else statements
Example 1 Write the program that will ask a user to type in a number and the program will give you information if the number is positive, less than 1000, greater than 1000, equal to 1000, negative, lower than -1000, greater than -1000, equal to -1000, or equal to 0.
Solution: This program has to ask a user to type in a number so we will use the input built-in function to ask a user to provide an input. Of course, the input function will return the type "string" so we have to use the float function to convert the input from string to float. This value will be assigned to variable name USER_INPUT.
if condition1:The previous block is a little bit complicated. The outer conditional contains three branches. Each branch contains three branches if/elif and else. The nested conditionals are complicated so the good idea is to avoid them if you can.
if condtion2:
#body
elif condition3:
#body
else:
#body
elif condition4:
if condtion5:
#body
elif condition6:
#body
else:
#body
else:
if condtion7:
#body
elif condition8:
#body
else:
#body
Example 1 Write the program that will ask a user to type in a number and the program will give you information if the number is positive, less than 1000, greater than 1000, equal to 1000, negative, lower than -1000, greater than -1000, equal to -1000, or equal to 0.
Solution: This program has to ask a user to type in a number so we will use the input built-in function to ask a user to provide an input. Of course, the input function will return the type "string" so we have to use the float function to convert the input from string to float. This value will be assigned to variable name USER_INPUT.
USER_INPUT = float(input("Enter a number:> "))Then the initial IF-ELIF-ELSE will check if the number is greater than 0 (positive), less than 0 (negative) or equal to 0. If the number is greater than 0 then the program will show as output "You have entered a positive number." and the program execution will enter the first block of nested conditionals (which will be explained and created later). If the number is less than 0 then the program will retrieve an output "You have entered a negative number." and the program execution will enter the second block of nested conditionals (which will be explained and created later). If the USER_INPUT variable value is equal to 0 then the program will show as output "The number is 0.".
if USER_INPUT > 0:Since we have to investigate if the positive number is less than 1000 or greater than 1000 or equal to 1000 under the IF statement we will create IF-ELIF-ELSE nested block. If the USER_INPUT value is less than 1000 the output will be "The number is less than 1000.". If the USER_INPUT value is greater than 1000 the output will be "The number is greater than 1000." If the USER_INPUT value is equal to 1000 the output will be "The number is 1000." The program code so far is given below.
print("You have entered a positive number.")
elif USER_INPUT < 0:
print("You have entered a negative number.")
else:
print("The number is 0.")
if USER_INPUT > 0:To investigate if the negative number is less than -1000, greater than -1000, or equal to -1000 under the ELIF statement we will create another IF-ELIF-ELSE nested block. If the USER_INPUT value is lower than -1000 then the output will be "The number is lower than -1000.". If the USER_INPUT value is greater than -1000 then the output will be "The number is greater than -1000.". If the USER_INPUT value is equal to -1000 then the output will be "The number is -1000." So this IF-ELIF-ELSE nested block is inside the ELIF block. The entire program code so far is given below.
print("You have entered a positive number.")
if USER_INPUT < 1000:
print("The number is less than 1000.")
elif USER_INPUT > 1000:
print("The number is greater than 1000.")
else:
print("The number is 1000.")
elif USER_INPUT < 0:
print("You have entered a negative number.")
else:
print("The number is 0.")
USER_INPUT = float(input("Enter a number:> "))To test all nested conditionals in created program we will type four different numbers each time we run the program and these are 500, 1500, -500, and -1500. Case I: The entered number is 500.
if USER_INPUT > 0:
print("You have entered a positive number.")
if USER_INPUT < 1000:
print("The number is less than 1000.")
elif USER_INPUT > 1000:
print("The number is greater than 1000.")
else:
print("The number is 1000.")
elif USER_INPUT < 0:
print("You have entered a negative number.")
if USER_INPUT < -1000:
print("The number is lower than -1000.")
elif USER_INPUT > -1000:
print("The number is greater than -1000.")
else:
print("The number is -1000.")
else:
print("The number is 0.")
Enter a number:> 500Case II: The entered number is 1500.
You have entered a positive number.
The number is less then 1000.
Enter a number:> 1500Case III: The entered number is -500.
You have entered a positive number.
The number is greater than 1000.
Enter a number:> -500Case IV: The entered number is -1500.
You have entered a negative number.
The number is greater than -1000.
Enter a number:> -1500There are three conditionals that we have not tested and these are if the entered number was equal to 1000, -1000, and 0. If these numberse were entered and saved to the USER_INPUT variable the following outputs would be obtained: "The number is 1000.", "The number is -1000.", and "The number is 0.", respectively.
You have entered a negative number.
The number is less than -1000.