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 chained conditional
num = 5
if num > 5:
print('Number greater than five')
elif num < 5:
print('Number less than five')
else:
print('The number is five')
#example nested conditional
num = '5' #assign a number to a variable as a string
if num.isdigit(): #if the string consists only of digits, then true will be returned
num = int(num) #convert string to number type
if num > 0: #this is a nested conditional because it is inside another condition
print('positive number')
elif num < 0:
print('number is negative')
else:
print("it's zero")
else:
print('Error. The string contains more than just numbers')
Comments
Leave a comment