Skip to content

Instantly share code, notes, and snippets.

@misterhtmlcss
Last active March 17, 2024 04:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save misterhtmlcss/bac4d3c3bb9b7d97e8a7ee4fe4f24f44 to your computer and use it in GitHub Desktop.
Save misterhtmlcss/bac4d3c3bb9b7d97e8a7ee4fe4f24f44 to your computer and use it in GitHub Desktop.
Describe the differences between chained and nested conditionals
"""
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.
✓ Describe the difference between a chained conditional and a nested conditional.
✓ Give your own example of each.
✓ Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional.
✓ Describe a strategy for avoiding nested conditionals (bottom).
"""
print('\n-----------------')
# Describe the difference between a chained conditional and a nested conditional.
# 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 vary in depth to create a more nuanced sequence of conditionals.
# Nesting is the key differentiator between a Chained Conditional and a Nested Conditional.
# Give your own example of each (Chained Conditional & Nested Conditional)
# Chained Conditional
def flow_if(bank_account):
if bank_account > 1000000:
print('Bye bye suckers!!')
elif bank_account < 0:
print('Will any suckers loan me money?')
else:
print("We are all poor together! Let's party in a park over marshmellows.")
flow_if(300000)
print('\n')
# Nested Conditional
def big_flow_if(taxes_due, overdue):
if overdue == True:
if taxes_due > 10000:
print('You are going to jail soon!')
elif 10000 > 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_if(10001, True)
big_flow_if(1001, True)
big_flow_if(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 conditional.
def compare_words(word1, word2):
if len(word1) > 3:
if len(word2) > 3:
print(f"{word1} is equal in size to {word2}")
else:
print(f"{word1} isn't equal in size to {word2}")
else:
print('They are not the same size!')
def compare_words2(word1, word2):
if len(word1) > 3 and len(word2) > 3:
print(f"{word1} is equal in size to {word2}")
else:
print(f"{word1} isn't equal in size to {word2}")
compare_words('jeff', 'jeff')
compare_words('jeff', 'jennifer')
print('\n')
compare_words2('jeff', 'jeff')
compare_words2('jeff', 'jennifer')
print('-----------------\n')
# Describe a strategy for avoiding nested conditionals.
# We are able to use operators (and, or, not) to give our conditional statements greater flexibily and this flexibility as you can see from above will sometimes allow for fewer nested conditionals. This is one accepted strategy.
# Another strategy is using functional statements that layer in different functions.
"""
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.
✓ Describe the difference between a chained conditional and a nested conditional.
✓ Give your own example of each.
✓ Give your own example of a nested conditional that can be modified to become a single conditional, and show the equivalent single conditional.
✓ Describe a strategy for avoiding nested conditionals (bottom).
"""
print('\n-----------------')
# Describe the difference between a chained conditional and a nested conditional.
# 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 vary in depth to create a more nuanced sequence of conditionals.
# Nesting is the key differentiator between a Chained Conditional and a Nested Conditional.
# Give your own example of each (Chained Conditional & Nested Conditional)
# Chained Conditional
def flow_if(bank_account):
if bank_account > 1000000:
print('Bye bye suckers!!')
elif bank_account < 0:
print('Will any suckers loan me money?')
else:
print("We are all poor together! Let's party in a park over marshmellows.")
flow_if(300000)
print('\n')
# Nested Conditional
def big_flow_if(taxes_due, overdue):
if overdue == True:
if taxes_due > 10000:
print('You are going to jail soon!')
elif 10000 > 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_if(10001, True)
big_flow_if(1001, True)
big_flow_if(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 conditional.
def compare_words(word1, word2):
if len(word1) > 3:
if len(word2) > 3:
print(f"{word1} is equal in size to {word2}")
else:
print(f"{word1} isn't equal in size to {word2}")
else:
print('They are not the same size!')
def compare_words2(word1, word2):
if len(word1) > 3 and len(word2) > 3:
print(f"{word1} is equal in size to {word2}")
else:
print(f"{word1} isn't equal in size to {word2}")
compare_words('jeff', 'jeff')
compare_words('jeff', 'jennifer')
print('\n')
compare_words2('jeff', 'jeff')
compare_words2('jeff', 'jennifer')
print('-----------------\n')
# Describe a strategy for avoiding nested conditionals.
# We are able to use operators (and, or, not) to give our conditional statements greater flexibily and this flexibility as you can see from above will sometimes allow for fewer nested conditionals. This is one accepted strategy.
# Another strategy is using functional statements that layer in different functions.
# Reference!
# Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press.
@Daniel-Nim
Copy link

Thanks for such amazing work.

@Crytographer1Cypher
Copy link

Excellent, clear and understanding.

@Crytographer1Cypher
Copy link

`` def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)

Write a new recursive function countup that expects a negative argument and counts “up” from that number. Output from running the function should look something like this:

countup(-3)
-3
-2
-1
Blastoff!

@fennyjuma
Copy link

this was so helpful,thanks

@MissLolaDavid
Copy link

Ow wow! You have saved me big time. Great explanation

@Solodazy
Copy link

Solodazy commented Dec 1, 2021

You guys are really awesome. Thanks alot, it was really helpful.

@Bernard078
Copy link

It really helpful and teachable. I Love it and didn't regret being on this site.

@aroldodev
Copy link

Thanks, it is really helpful.

@robertopioi850
Copy link

Yah it has been really helpful thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment