Discussion Assignment Unit3
Discussion Assignment Unit3
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.
The code and its output must be explained technically whenever asked. The explanation
can be provided before or after the code, or in the form of code comments within the code.
For any descriptive type of question. Your answer must be at least 150 words.
End your discussion post with one question related to programming fundamentals learned
in this unit from which your colleagues can formulate a response or generate further
discussion. Remember to post your initial response as early as possible, preferably by
Sunday evening, to allow time for you and your classmates to have a discussion.
[Downey, Allen. (2015). Think Python] Chained Conditionals are used to check multiple conditions in a
row. In situations there could be more than two possibilities and it more than two branches are needed,
chained conditionals can be used to express such computations.
For example, the following code uses a chained conditional to check if a number is positive, negative or
zero.
if number > 0:
print("The number is positive")
elif number < 0:
print("The number is negative")
else:
print("The number is zero")
The first condition checks if the number is greater than zero, for example “4”. The code prints a message
saying that “the number is positive”.
The second condition checks if the number is less than zero, for example “-4”. The code prints a message
saying that “the number is negative”.
The las condition “else” checks if the number is zero, for example “0”. The code prints a message saying
that “the number is Zero”.
[Downey, Allen. (2015). Think Python] Nested conditionals are used to check one condition inside
another. One condition can also be nested within another condition. Below shows an example of how a
nested conditional would look like.
if number % 3 == 0:
print("The number is divisible by 3")
else:
print("The number is not divisible by 3")
The first condition checks if the number is divisible by 3. If number is, then the code prints a message
saying that “The number is divisible by 3”
If number is not divisible by 3, then the code prints a message saying that “The number is not divisible
by 3”.
Therefore, chained conditions are often used when one needs to check multiple conditions in a row, and
Nested conditions are used when one needs to check one condition inside another.
Yes, deeply nested conditionals can become difficult to read. This is because it can be difficult to follow
the logic of the code when there are many levels of indentation. It can also be difficult to see which
conditions are being checked and what the results of those checks are.
To avoid this problem, it is best to keep your conditionals as simple as possible. If you need to check
multiple conditions, it is often better to use an `elif` statement instead of nesting multiple `if`
statements. You can also use the `and` and `or` operators to combine multiple conditions into a single
expression.
This code can be rewritten using the “add” Operators, as shown below.
This code is much easier to read because it is easier to follow the logic of the checks. It is also easier to
see which conditions are being checked and what the results of those checks are. For example, from the
code a user is able to know that the condition checks if “number is greater than 0 and also at the same
time number is less than 100” compare to the first code where two if statements are used.
Reference:
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press