Answer to Question #184682 in Python for FORTUNE AIDOO

Question #184682

Describe the difference between a chained conditional and a nested conditional. Give your own example of each. 


Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested conditionals. Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional. 


1
Expert's answer
2021-04-24T11:28:44-0400

Describe the difference between a chained conditional and a nested conditional. Give your own example of each. 

The main difference between chained conditional and nested conditional is nesting. Chained conditional is a conditional that contains a series of alternative branches using if, elif and else statements that are all indented at the same depth. There is no nesting in chained conditional. On the other hand, nested conditional is where one conditional is nested within another conditional. The if, elif and else statements in nested conditional have varying depths.

Example of chained conditional

 

grade= input("Enter your grade: ")

if grade=='A':

  print("You have an excellent grade")

elif grade=='B':

  print("You have a good grade")

elif grade=='C':

  print("You have a fair grade")

elif grade=='D':

  print("You have a poor grade")

elif grade=='E':

  print("You have failed")

else:

  print("You have entered an invalid grade")

 

 

Example of nested conditional

 
num=int(input("Enter a number: "))

if num==0:

    print(num, "is zero")

else:

    if num<0:

        print(num, "is negative")

    elif num>0:

        print(num, "is positive")
 

 

Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested conditionals.

The strategy for avoiding nesting condition is by use of logical operators such as ‘and’, ‘or’ and ‘not’.

Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional. 

Example of nested conditional

 
num=int(input("Enter a number: "))

if num>0:

    if num%2==0:

        print(num, "is a positive even number")

    else:

        print(num, "is a positive odd number")

elif num<0:

    if num%2==0:

        print(num," is a negative even number")

    else:

        print(num, "is a negative odd number")

else:

    print(num, "is zero")
 

 

Equivalent single conditional

 
num=int(input("Enter a number: "))

if num>0 and num%2==0:

    print(num, "is a positive even number")

elif num>0 and num%2 !=0:

    print(num, "is a positive odd number")

elif num<0 and num%2==0:

    print(num, "is a negative even number")

elif num<0 and num%2 !=0:

    print(num, "is a negative odd number")

else:

    print(num," is zero")
 

 


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Kivumbi Richard
26.04.24, 17:00

Good

Laigwanani
24.04.22, 21:24

Commendable work. I like the way the work is well articulated even for a beginner to understand

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS