Answer to Question #99682 in Python for HAIDER

Question #99682
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
2019-12-01T13:09:13-0500
# i'ts nested conditional using if afte else: and creates more code
x = 0
if x < 0:
    print("This number ",  x, " is negative.")
else:
    if x > 0:
        print(x, " is positive")
    else:
        print(x, " is 0")

#it's chained conditoinal using elif and return sample result and creates less code
if x < 0:
    print("This number ",  x, " is negative.")
elif (x > 0):
    print(x, " is positive")
else:
    print(x, " is 0")
# If you need that constructions [else: if()] you can replace it with elif

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS