Describe the difference between a chained conditional and a nested conditional. Give your own example of each. Do not copy examples from the textbook.
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. Do not copy the example from the textbook.
Chained Conditional: A chained conditional is when you use if/elif/else flow controls and they are all indented to the same depth. No nesting.
Nested conditional: A nested conditional is when you use if/elif/else flow controls and the conditionals.
Nesting is the key differentiator between a Chained Conditional and Nested Conditional.
Chained Conditional:
def flow 1f(bank_account):
If bank account > 1000000:
print("Bye bye suckers!!")
elif bank account < 0:
print(" any suckers loan money?)
else:
print(“We are all poor together! Let's party in park”.)
flow_if(300000)
print("\n')
Nested Conditional:
def big flow Of(taxes due, overdue):
if overdue - True:
if taxes_due > 10000:
print("You are going to jail soon!)
elif 1000e > taxes_due > 1000:
print("We are thinking your car would look nice in our Inspectors yard")
else:
print("We'll give you some time, before we call the cops")
else:
print("We are all friends, but don't ever be late")
big flow of(10001, True)
big Flow Of(1001, True)
big Flow Of(1001, False)
print("\n')
Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single condition:
def compare words (word1, word 2):
if len(word1) > 3:
If len(word2) 3:
print(f"{word1) is equal in size to (word 2)
else:
print("{word1) isn't equal in size to {word2})
else:
print("They are not the same size!” )
def compare_words (word1, word2):
if len(word1) > 3 and len(word2) > 3:
print(f"(wordi) is equal in size to{word2}")
else:
print("{wordi) Isn't equal in size to {word2}")
compare_words("Jeff", "Jeff")
compare_words("Jeff", Jennifer')
print('\n')
compare_words2('Jeff', 'feff')
compare words2("jeff', 'jennifer')
print(“-----------------------”)
Comments
Leave a comment