0% found this document useful (0 votes)
5 views3 pages

Discussion Assignment Unit3

The document discusses the differences between chained and nested conditionals in programming, providing examples for each. It emphasizes the readability issues associated with deeply nested conditionals and suggests strategies to simplify them, such as using 'elif' statements and logical operators. The document concludes with a question about using recursion to solve the Fibonacci Sequence.

Uploaded by

SAYILE JAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Discussion Assignment Unit3

The document discusses the differences between chained and nested conditionals in programming, providing examples for each. It emphasizes the readability issues associated with deeply nested conditionals and suggests strategies to simplify them, such as using 'elif' statements and logical operators. The document concludes with a question about using recursion to solve the Fibonacci Sequence.

Uploaded by

SAYILE JAMES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Discussion Assignment

Welcome to Unit 3 Discussion Forum.

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.

#program that checks in number is positive, negative or zero

number = int(input("Enter a number: ")) #accepts user input

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.

#Program cheks if number is divisible by 3


number = int(input("Enter number: "))

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.

Here is an example of a deeply nested conditional:

#the following code uses a nested conditional to check if a number is


positive, negative, or zero:

number = int(input("Enter a number: "))

#statement checks whether if number is greather 0 and also checks if it


less than 100
if number > 0:
if number < 100:
print("The number is between 0 and 100")
else:
print("The number is not between 0 and 100")

This code can be rewritten using the “add” Operators, as shown below.

number = int(input("Enter a number: "))

#statement checks whether if number is greather 0 and also checks if it


less than 100
if number > 0 and number < 100:
print("The number is between 0 and 100")
else:
print("The number is not between 0 and 100")

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.

QN: How can I use recursion to solve the Fibonacci Sequence ?

Reference:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy