Discussion Assignment unit 3
Discussion Assignment unit 3
Nested Conditionals
Both chained conditionals and nested conditionals are ways to handle multiple conditions in a
Chained Conditional:
A chained conditional uses multiple if, elif, and else statements to evaluate different conditions
in sequence. It is linear, and is easy to read. The first condition that evaluates to True then the
age = 25
else:
Output Explanation:
If age < 18 is true, then it prints “You are a minor.” skips the rest. If the first condition is False
then it will check the second condition “Otherwise, it executes the else block and prints “You are
a senior."
Nested Conditional:
A nested conditional places one conditional statement inside another, forming a hierarchy of
decisions. Nested conditionals can become difficult to follow as the depth increases.
age = 18
grade = 85
else:
else:
Output Explanation:
The outer if checks the age condition. If true, the inner if checks the grade. Depending on both
1. Use logical operators (and, or) to combine conditions into a single conditional.
Nested Conditional:
age = 18
grade = 85
else:
else:
age = 18
grade = 85
else:
Explanation:
The nested structure is flattened by combining conditions with and. Additional conditions are
Discussion Question:
When would you prefer a nested conditional over a chained conditional, even though the latter is
Downey, A. B. (2015). Think Python: How to think like a computer scientist (2nd ed.).
O'Reilly Media.