Answer to Question #298385 in Python for Phikem

Question #298385

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




1
Expert's answer
2022-02-16T08:11:00-0500
# 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.
# Chained conditional on an example when we need to get some command from the user
inputCommand = 'login' #to this variable for the command that the user entered (for example, through input())

if inputCommand == 'register':
    print('Redirect to registration page')
elif inputCommand == 'login':
    print('Redirect to login page') #this line will be printed
else:
    print('Unknown command')

print('\n')
# Nested conditional example. code checks the sex, age and health of a person to find healthy men over 18 and under 27 years old to send army

people = [{'name': 'John', 'sex': 'male', 'age': 21, 'health': 'poor'}] #data

for human in people: #iterate over the array data, because there can be more than one object
    if human['sex'] == 'male':
        print('Ok. This man') #will be printed
        if human['age'] >= 18:
            if human['age'] < 27:
                print('Ok. Age suitable') #will be printed
                if human['health'] == 'good':
                    print('Ok. Health is good')
                    print(f'{human["name"]} goes to the army')
                else:
                    print('This man is in poor health. ') #will be printed
            else:
                print('The man is too old')
        else:
            print('The guy is too young')
    elif human['sex'] == 'female':
        print("Women don't join the army ")
    else:
        print('Some error in the data')

print('\n')
# 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.
# We already had a situation in our code when it was possible to apply the operator and. When to check age (over 18 and under 17)

age = 25

if age > 18 and age < 27:
    print('ok')
else:
    print('Wrong age')

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
New on Blog
APPROVED BY CLIENTS