CS 1101 - AY2022-T2 - Discussion Unit 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

CS 1101 Programming Fundamentals - AY2022-T2


Dashboard / My courses /
CS 1101 - AY2022-T2 / 25 November - 1 December /
Discussion Forum Unit 3 /
Discussion Unit 3

 Search forums

Discussion Forum Unit 3


Discussion Unit 3

Settings

Display replies in nested form

The cut-off date for posting to this forum is reached so you can no longer post to it.

Discussion Unit 3
by Leonidas Papoulakis (Instructor) - Wednesday, 10 November 2021, 7:09 AM

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.

70 words

Permalink

Re: Discussion Unit 3


by Stephen Chuks - Saturday, 27 November 2021, 7:06 AM

Unit 3 Discussion

In chained conditionals, the conditional statement(if) and the alternate execution or branches (elif/else) are indented on the
same depth meaning they are separate blocks of codes While nested conditional statements are conditional statements where
other conditional statements are put within another conditional statement by using indentation.

This is an example of a chained conditional

test=10
exam=40
if test+exam>=70: 
  print('You passed with distinction')
elif test+exam>=40<=69:
  print('You passed with C')

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 1/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

else:
  print('You failed, try harder next time')
#The above code gave the elif condition as output.

#Putting the same code in a nested form


test=10
exam=40
if test+exam>=70:
  print('You passed with distinction')
else:
  if test+exam>=40<=69:
      print('You passed with C')
  else:
      print('You failed, try harder next time')
#The elif was also returned as output in the above example.

an example of a nested conditional statement that can be modified to a single line

test=40
exam=40
if test+exam>=80:
  if test+exam<=90:
      print('You passed with A-')
#The two nested conditional statements can be put in a single line .

test=40
exam=40
if test+exam>=80 <=90:
      print('You passed with A-')

#They both produced the same results.

181 words

Permalink Show parent

Re: Discussion Unit 3


by Wilmer Portillo - Sunday, 28 November 2021, 6:07 PM

Hi stephen, clear and concise discussion. Great that you kept the same example for both sections of the discussion. Keep it
up!
22 words

Permalink Show parent

Re: Discussion Unit 3


by Luke Henderson - Monday, 29 November 2021, 2:33 AM 

Nice and concise explanation of chained and nested conditionals. Great use of different colours to make the comments
stand out. You covered all the necessary criteria. Keep up the good work.

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 2/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

31 words

Permalink Show parent

Re: Discussion Unit 3


by Yahya Al Hussain - Tuesday, 30 November 2021, 1:52 AM

very simple and easy to understand! also the colors help make it easier to read
15 words

Permalink Show parent

Re: Discussion Unit 3


by Ahmet Yilmazlar - Tuesday, 30 November 2021, 11:00 AM

Hi stephen, clear and concise discussion


6 words

Permalink Show parent

Re: Discussion Unit 3


by Simon Njoroge - Tuesday, 30 November 2021, 11:40 AM

Your input into the discussion is clear. Thank you for your input. Your explanation for the different type of conditions is
quite clear. The examples you gave for the codes are equally good.
33 words

Permalink Show parent

Re: Discussion Unit 3


by Fouad Tarabay - Tuesday, 30 November 2021, 11:50 AM

Hello Stephen,

The explanation of the difference between nested and chained conditionals was easy to understand. Moreover, using the
same code to spot on the differences between them was great. well done!!
32 words

Permalink Show parent

Re: Discussion Unit 3


by Crystal Noralez - Wednesday, 1 December 2021, 9:42 PM

Hi well done. keep up the great job. You explain very well.
12 words

Permalink Show parent

Re: Discussion Unit 3


by Mayowa Ogunyale - Wednesday, 1 December 2021, 9:47 PM

Hi Stephen

Good work. well demonstrated idea
7 words

Permalink Show parent


https://my.uopeople.edu/mod/forum/discuss.php?d=640871 3/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Re: Discussion Unit 3


by Luke Henderson - Saturday, 27 November 2021, 6:04 PM

A chained conditional is linear in nature. From the top to the bottom there are a series of individual conditions which are
evaluated one after the other as can be seen below:

fingers = 10

if fingers > 10:

   print(“you have abnormal hands”)

elif fingers < 10:

   print(“you seem to be missing a few          fingers”)


else fingers == 10:

     print(“you have the correct amount of fingers”)

If one condition in the series evaluates to true, the statement inside the condition is executed and the conditions below it are
discarded.

In the case of nested conditions we can think of the structure as a tree. When we nest conditions we are making branch like
structures. Starting from the base of the trunk the program goes to where there is the first split in the trunk. If the first
condition is true the program will continue to evaluate the further branches or conditions nested within the first branch. If
none of those conditions are true it will move on to the next main branch, for example:

dadsAge = 56

if dadsAge > 60: #statement evaluates to     false so the nested condition will be skipped

    print(“dad is old”)

    if dadsAge == 65:

         print(“dad is older than I thought”)

if dadsAge < 60: # this condition evaluates to true and so continues to check the nested condition

     print(“dads not too old”)

     if dadsAge == 56: # this condition is also true and the statement will be executed

           print(“dad is 56 years old”)

Deeply nested conditions can be avoided by using the operators “and” and “or” to check multiple criteria in one single
condition. We can use the following example to demonstrate this like so:

# nested condition

val = 0.5

if (val > 0):

    if (val < 1):

          if (type(val) == float):

                print("success")


# single condition

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 4/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

if (val > 0 and val < 1 and type(val) == float):

      print(“success”)

note: cant seem to correct the indentation here


323 words

Permalink Show parent

Re: Discussion Unit 3


by Leameo Gilah - Sunday, 28 November 2021, 4:31 PM

Nice explanation, you answer the question straight to the core, but would better if you cite the sources next time.nkeep
learning bro.
22 words

Permalink Show parent

Re: Discussion Unit 3


by Wilmer Portillo - Sunday, 28 November 2021, 6:09 PM

Great discussion. Your analogies are on point and even cleared up a few doubts I Still had. I always look forward in going
over your discussion. You have a great strenght in this subject. Keep it up!
37 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Monday, 29 November 2021, 6:21 AM

Hi, Luke, your explanation is very nice overall. I have learned from you how to avoid nested conditions and that is great.
The examples you give are also clear and detailed.
31 words

Permalink Show parent

Re: Discussion Unit 3


by Hyun Oh - Tuesday, 30 November 2021, 1:12 AM

Integrating the "type" command for your example enlightened my knowledge. All of your explanations are clear and
helpful. Thank you for the posting!
23 words

Permalink Show parent

Re: Discussion Unit 3


by Yahya Al Hussain - Tuesday, 30 November 2021, 1:54 AM

the answer is well explained, code is clean and simple


10 words

Permalink Show parent


Re: Discussion Unit 3


by Ahmet Yilmazlar - Tuesday, 30 November 2021, 11:00 AM

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 5/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Hi luke, clear and concise discussion


6 words

Permalink Show parent

Re: Discussion Unit 3


by Crystal Noralez - Wednesday, 1 December 2021, 9:44 PM

Hey Luke,

I see you went in dept in expressing the difference both. A job well done.
17 words

Permalink Show parent

Re: Discussion Unit 3


by Leameo Gilah - Sunday, 28 November 2021, 2:29 AM

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.

Answer: The basic difference between chained conditionals, and nested conditionals, stands

on the fact that chained conditionals don't separate commands in only two different

branches, but even more, and it does so by using the function "elif", which is the

abbreviation of "else if". Nested conditionals, on the other hand, are conditionals

found within the statements, and they create branches within branches, thus

making it challenging to quickly read more complex coding (Downey A., 2015).

Here are a couple of examples that will make both clear:

Chained conditional

a = 15

if 10 < a < 20 :

print("The temperature is cold")

elif 20 < a < 30 :

print("The temperature is warm")

else:

print("Out of bounds")

The temperature is cold

>>> or
a = 3

if 10 < a < 20 :

print("The temperature is cold")

elif 20 < a < 30 :

print("The temperature is warm")

else:


print("Out of bounds")

Out of bounds

>>> Nested conditional

a = 22

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 6/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

if 10 < a < 20 :

print("The temperature is cold")

else:

if 21 < a < 30 :

print("The temperature is warm")

else:

print("Out of bounds")

The temperature is warm

>>> or
a = 17

if 10 < a < 20 :

print("The temperature is cold")

else:

if 21 < a < 30 :

print("The temperature is warm")

else:

print("Out of bounds")

The temperature is cold

>>> As you can already see, this very simple code already embraces the reversibility of

these two types of conditionals. Honestly, I believe that there is no "effective"

strategy to avoid using nested conditionals, it's just a matter of attention. Both

types of conditionals can have the same exact purpose, the main difference is that

nested conditionals can make a more complex code far more clumsy and difficult

to read (Downey A., 2015).

Reference

Downey, A. (2015). Think Python, how to think like a computer scientist,

nd Edition. This book is licensed under Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0).
400 words

Permalink Show parent

Re: Discussion Unit 3


by Wilmer Portillo - Sunday, 28 November 2021, 6:12 PM

Good job in your discussion. I like the fact that you can use relevant examples that helps to further understand the current
subject. Keep it up!
26 words

Permalink Show parent

Re: Discussion Unit 3


by Luke Henderson - Monday, 29 November 2021, 2:38 AM

You explained chained and nested conditionals correctly. Good that you pointed out that elif stands for elseif, which may
not be apparent to everybody. You used references, but be careful when copying and pasting the source reference as you
included the creative commons license which is not necessary.
48 words

Permalink Show parent


Re: Discussion Unit 3
by Stephen Chuks - Monday, 29 November 2021, 3:03 AM

Your explanations are good. You noted some very important points but your code was not clear because of lack of
https://my.uopeople.edu/mod/forum/discuss.php?d=640871 7/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

indentation. It's understandable though - pasting your codes on this comment box rearranged the code but this post is
strictly about the use of nesting and indentation so it's particularly important that you should try show the indentation.
Overall, you did good.
62 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Monday, 29 November 2021, 6:31 AM

Hi, Leameo, your explanation is good overall. You have explained the difference between chained and nested conditionals
clear. But I have noticed that you explained there is no way of avoiding nested conditionals. As our classmate, Luke
explained there is a way to avoid that problem and you can learn from his writings as I did.
56 words

Permalink Show parent

Re: Discussion Unit 3


by Leameo Gilah - Monday, 29 November 2021, 11:19 AM

Thanks to everybody, I appreciate your comments on my work atleast I can use it to improve next time. I hope after
school shall have closed, we all can work on a project together.
34 words

Permalink Show parent

Re: Discussion Unit 3


by Ahmet Yilmazlar - Tuesday, 30 November 2021, 11:01 AM

Hi leamo, clear and concise discussion


6 words

Permalink Show parent

Re: Discussion Unit 3


by Mayowa Ogunyale - Wednesday, 1 December 2021, 9:48 PM

Hi Leamao

making python looking simple. this is well done job.


11 words

Permalink Show parent

Re: Discussion Unit 3


by Simon Njoroge - Sunday, 28 November 2021, 11:23 AM

Discussion – Unit 3

The term chained condition refers to setting conditions that are of the same level in python. The level of indenting of all the if,
elif and else condition is the same. This means their execution is more or less in the same level of comparison for in all 
conditions. There is basically no condition within the first condition.

Example of a chained condition is

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 8/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Nested condition refers to a condition that has more than one level. The if , elif and else conditions are of different levels. A
condition is placed within another condition and therefore if first condition is not met then execution of the condition is
skipped and the next condition is executed. We normally indent the sub-conditions in a different position in python.

Example of a nested condition

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.

The best strategy to avoid nested condition based on the examples below is to reduce the number of conditions in an
argument. Compiling the major arguments in may reduce the number of conditions one is using and therefore help to avoid a
nested condition. Look at the example below:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 9/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

The condition has two levels the first level checks if the test has been approved, if True the program will go ahead and execute
the fist if condition with all its sub-conditions. If the first condition is not met , the else condition is executed.

I have reduced the conditions in the nested condition above and below are the results of a the program with chained
condition.

Reference

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist (2nd ed.). Green Tea

Press

Khan Academy (2021, January 1). Nested conditionals (if/else/if) | AP CSP (article).
https://www.khanacademy.org/computing/ap-computer-science-principles/programming-101/boolean-logic/a/nested-
conditionals

Kodify.Net(2019). Python’s nested if statements: if code inside another if statement. Kodify.Net. https://kodify.net/python/if-
else/nested-if/

332 words

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 10/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Permalink Show parent

Re: Discussion Unit 3


by Luke Henderson - Monday, 29 November 2021, 2:42 AM

Nice explanation of chained and nested conditions. Resources clearly presented and in accordance with the APA format.
Make sure to double check your work for spelling mistakes. Nice use of pictures but please make sure to zoom in or
increase the size of the text in your IDE as it is a little hard to read. Overall good work.
59 words

Permalink Show parent

Re: Discussion Unit 3


by Stephen Chuks - Monday, 29 November 2021, 2:43 AM

Great work you have there. Your explanations are straight and concise. and your coding is creative.
16 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Monday, 29 November 2021, 6:36 AM

Hi, Simon, very nice explanation as well as the examples. You have also provided helpful references with APA standards. I
have learned a lot from you. thank you.
28 words

Permalink Show parent

Re: Discussion Unit 3


by Leameo Gilah - Monday, 29 November 2021, 11:25 AM

Nice explanations, just that the codes written in your editor are little harder to read, please improve the readibility of your
codes next time.
24 words

Permalink Show parent

Re: Discussion Unit 3


by Hyun Oh - Tuesday, 30 November 2021, 12:48 AM

This is a great posting with creativity and detailed explanations. Ultimately, I've learned from your posting what I wanted
to do but was not able to make it happen for my examples and that was grafting function into conditionals. I was kept
trying to use the "print" command to get the outcome and did not work out, but now I have a better understanding of
both last week's and this week's learning materials. Thank you for the posting.
78 words

Permalink Show parent

Re: Discussion Unit 3


by Yahya Al Hussain - Tuesday, 30 November 2021, 1:56 AM

the answer looks clean and well explained, but the pictures could have been bigger .
15 words

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 11/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 5:49 AM

Hey Simon, this is incredible. You pointed out the difference well. I also find one of your references helpful, thanks for
sharing, and keep the great work in learning to program.
31 words

Permalink Show parent

Re: Discussion Unit 3


by Crystal Noralez - Wednesday, 1 December 2021, 9:46 PM

Great job in your discussion. You explain the difference between chained and nested condition very well.
16 words

Permalink Show parent

Re: Discussion Unit 3


by Mayowa Ogunyale - Wednesday, 1 December 2021, 9:49 PM

Very well explained. More of this break down


8 words

Permalink Show parent

Re: Discussion Unit 3


by Fouad Tarabay - Sunday, 28 November 2021, 2:46 PM

The difference between nested conditionals and chained conditionals is that in the chained conditionals when the condition is
not true the program continue checking the other conditions since they are at the same indentation while in the nested
conditionals the condition has to be true so the program moves to execute the other conditions.

Example of chained conditionals:

def f(x,y,z):

if x>y:

print(x," grater than ",y)

elif x>z:

print(x," grater than ",z)

else:

print(x," less than ",y," and ", z)

f(2,3,4)

Example of nested conditionals:


def func(x,y,z):

if x==0:

if y>z:

if y>10:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 12/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

print("Finish")

else:

print("Not finished")

else:

print(y," less than ",z)

else:

print(x, " isn't = 0")

func(9,5,7)

To avoid nested conditionals we can use the conditional operators like: "and"

example:

This code:

if x>4:

if y<5:

print('hello')

can be written as:

if x>4 and y<5:

print('hello')

References:

Downey, A. (2015). Think Python: How to think like a computer scientist. Conditionals and Recursion. (p 41).
156 words

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 5:54 AM

This is nice, you really think like a computer scientist. I find the distinction between nested and chained conditionals
helpful. Keep it up!
23 words

Permalink Show parent

Re: Discussion Unit 3


by Janelle Bianca Cadawas Marcos - Tuesday, 30 November 2021, 1:39 PM

Hi Fuoad,

That's a good explanation about chained conditionals versus nested conditionals. I suggest you format your post a little
more, it's kind of hard to read the code since there's no spacing on them. Good work though.
38 words

Permalink Show parent

Re: Discussion Unit 3


by Wilmer Portillo - Sunday, 28 November 2021, 5:49 PM

Discussion Assignment:

Part 1:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 13/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Chained Conditionals and a nested conditional are both considered to be statements in python. They both share if/elif/else
flow controls. Looking closely at what the difference is, we can say that in “chained Conditional” the flow controls are all
intended to the same depth, whereas, “Nested Conditional”, can include the flow controls at various different depths. 

Example of Chained Conditional: Format of depths are aligned. 

Example of a Nested Conditional: Format of dephts not aligned. 

Part 2:

As studied in this week’s material, deeply nested conditionals can become difficult to read and therefore we can focus on using
the logical operators like “and”, “or” and “not” to make it a bit more digestible to understand and follow.

Example of nested conditional than contain two branches. A simple statement can be noted on the first branch. The second
branch contains another if statement, which has two branches. Those two branches are both simple statements, although they
could have been conditional statements too.

Simplified:

References:

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. This book is licensed under Creative
Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0).

197 words

Permalink Show parent


Re: Discussion Unit 3
by Stephen Chuks - Monday, 29 November 2021, 3:45 AM

very good points from you Wilmer. Your explanations are professional and shows you know what you're talking about. and
https://my.uopeople.edu/mod/forum/discuss.php?d=640871 14/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

your codes are easy to read. nice one


27 words

Permalink Show parent

Re: Discussion Unit 3


by Juan Duran Solorzano - Monday, 29 November 2021, 11:27 AM

Very nice explanation, your programs are simple and concise, good citation, and clearly you know what are you talking
about. Great job.
22 words

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 5:59 AM

Great work in reflecting the distinction between chained and nested conditionals. I would like to recommend you to
practice using script mode and present your program in scripts than using interactive mode. Nevertheless, you did it well.
37 words

Permalink Show parent

Re: Discussion Unit 3


by Simon Njoroge - Tuesday, 30 November 2021, 11:46 AM

Your effort to this question is extremely good. Your input makes us understand the topic even more. You have given simple
but sensible examples on the conditions. Your effort has not gone to waste. You have made me learn more. Keep it up!
43 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Monday, 29 November 2021, 4:55 AM

     
Discussion Forum Unit
3

Describe
the difference between a chained
conditional and a nested
conditional. Give your own example of each.

Chained
conditionals
are flows of programming executions, where we use to check multiple conditions
of possibilities. For
example, we have learned from chapter 5 of the book Think
Python an if…...else is used to check two conditions (Downey,
2015, p. 41).

Below
is an example of chained conditionals. Here I am going to use chained
conditionals that check final exam pass marks for
an English course.

The script is as follows: -

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 15/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Output
is as follows

Nested
Conditionals are
flows of programming executions inside other conditional execution. “One
conditional can also be
nested within another” (Downey, 2015, p. 42).

Here I am going to use nested if …… else condition to display


student Grade Letter and their rank of excellence by accepting
Mark of the
student and status of the student from the user.

                    The code looks like as


below

        

The output is as below

Output
for the status of disability=no

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 16/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

                 
          Output
for the status of disability=yes

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 17/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

  

Reference

Downey, A. (2015, pp. 41-42 ). Think


Python: How to Think Like a Computer Scientist (2nd ed., Version 2.4.0). Green Tea Press.

201 words
Tags:
Unit 3 Discussion Forum
Discussion Forum Unit 3.docx
P li k Sh

Re: Discussion Unit 3


by Juan Duran Solorzano - Monday, 29 November 2021, 11:11 AM

Great job, I like the way you created your examples, the examples are very clear, easy to read and to understand the flow

of the data.
26 words

Permalink Show parent

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 18/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Re: Discussion Unit 3


by Samrawi Berhe - Tuesday, 30 November 2021, 8:31 AM

Thank you for your comments.


5 words

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 6:05 AM

Great work in using reasonable examples. I learned some concepts from your programs regarding the topic. Thanks for
sharing.
19 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Tuesday, 30 November 2021, 8:32 AM

Thank you for your comments.


5 words

Permalink Show parent

Re: Discussion Unit 3


by Simon Njoroge - Tuesday, 30 November 2021, 11:52 AM

You have put a great effort and shed a lot of light in this discussion. Your examples are totally simple and very clear. Thank
you for your input in the discussion. You clearly understand the topic. You handled explanations extremely well. Keep it up!
44 words

Permalink Show parent

Re: Discussion Unit 3


by Janelle Bianca Cadawas Marcos - Tuesday, 30 November 2021, 1:41 PM

Hi Samrawi,

The way you formatted you post makes it really easy to read and understand. It shows how much time you put into this.
Nicely done, good job!
29 words

Permalink Show parent

Re: Discussion Unit 3


by Peter Odetola - Tuesday, 30 November 2021, 5:56 PM

Hello Samrawi,

I really love the way you showed your script and the output in the chained conditionals. The work is actually voluminous

for the nested conditionals. Nevertheless, I appreciate the efforts you put into it. Thank you
38 words

Permalink Show parent

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 19/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Re: Discussion Unit 3


by Juan Duran Solorzano - Monday, 29 November 2021, 6:36 AM

Describe the difference between a chained conditional and a nested conditional. Give your own example of each.

The difference between chained and nested conditional is that chained conditional can be used to check multiple conditions at
the same time, using if, elif, and else. We can chain as many conditions as we need using Elif, to do the action required by each
condition.

In the following example, I chained 4 conditions checking different names printing different characteristics per name, if the
name typed using print function was in one of the conditions would print a characteristic, otherwise will print sorry I don’t
know you.

# Chanied conditionlas

name = input("What is your name?")

if name == "John":

  print("Hello, John, you are my friend")

elif name == "Mary":

  print("Hello, Mary, you are beautiful")

elif name == "Mark":

  print("Hello, Mark, You know how to play football.")

elif name == "Mike":

  print("Hello, Mike, you are very tall.")

else:

  print("Sorry I don't know you.")

#Nested conditionals are a piece of code where there is an if-else inside of an if-else, this only will be checking one condition
at the time. The problem with this is when we have a lot of nesting conditionals the indentation can be very tricky as we have
to respect the python syntax rule of spaces. 

In the following example, there are 3 questions using the input function to get some information from the user and after
getting the information, the nested conditionals will evaluate the data entered, if the gender is a woman will say 'You are a
woman, named user's name', then will check if the age is greater than 30, if it is will print you are a woman older than me,
otherwise will print the opposite. Same with the man's condition. 

# Nested conditionals example

name = input("What is your name?")

gender = input("man or woman")

age = int(input("How old are you"))

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 20/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3
if gender == 'boy':

  print("You are a man, named", name)

  if age > 30:

      print("You are a man older than me")

  else:

      print("You are a man younger than me")

else:

  print("You are a woman name", name)

  if age > 30:

      print("You are woman older than me")

  else:

      print("You are a woman younger than me")

This example has only one nested condition, as it was easier to explain you could add as many nested conditionals as you
need but as I mentioned before it will be very hard to keep track of the indentation, even if we used a IDE to help us to format
the program.

The strategy to keep the nested conditional easy to read we could use logical operators(and, or, not), these operators will help
us to create one condition in a single line, this way we will not get confused while reading the code and debugging if we need
to. 

Example with logical operators:

color1 = input ("What color do you like?")

color2 = input ("What color do you like?")

color3 = input ("What color do you like?")

# This conditional checks that color1 is equal to red and color2 is equal to blue and color3 is equal to purple,

# if any of this condition is false will automatically skip the line and will run the command from else,

# Otherwise if both conditions are true will print I like red, blue and purple too.

if color1 == "red" and color2 == "blue" and color3 == "purple":

print("I like red, blue and purple too.")

else:

print("We have different color taste")

Same program using nested conditionals, using the same variables from the previous exercise, color1, color2 and color3 :

if color1 == "red":

if color2 == "blue":

if color3 == "purple":

print("I like red, blue and purple too.")

else:

print("We have different color taste")

629 words

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 21/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

P li k Sh

Re: Discussion Unit 3


by Hyun Oh - Tuesday, 30 November 2021, 1:52 AM

Hi Juan, I really like your examples because they are pretty different than mine in a good way. However, it would have
been even better if you had included the outputs for all your inputs. For example, I may be missing something, but when I
copied and pasted your third example and I am not getting either output from both conditional. I would love to see the
outputs. Thanks for the posting.
72 words

Permalink Show parent

Re: Discussion Unit 3


by Juan Duran Solorzano - Tuesday, 30 November 2021, 1:57 AM

Thank you, the 3 example as is mentioned is using the same color inputs from the previous example

If you run only the conditionals nothing will happen or will say the color1, color2 and color3 are not defined.

The examples work with the inputs.


44 words

Permalink Show parent

Re: Discussion Unit 3


by Hyun Oh - Tuesday, 30 November 2021, 7:50 PM

I've been debugging and finally found the solution to the issue. All I had to do was remove this part --> ("What
color do you like?"). Great examples!
28 words

Permalink Show parent

Re: Discussion Unit 3


by Samrawi Berhe - Tuesday, 30 November 2021, 8:38 AM

Great work Juan, you have explained both chained and nested conditional clearly. I have learned more about chained
conditional from you. You have also demonstrated them with neat examples.
29 words

Permalink Show parent

Re: Discussion Unit 3


by Janelle Bianca Cadawas Marcos - Tuesday, 30 November 2021, 1:46 PM

Hi Juan,

I really like the format of your post, it's very clean and easy to understand. It's nice to see the explanations you put as well.
This shows the time you put into it. I'm a little biased on your nested conditionals example though since it only assumes
two genders, would be nice if you made the woman an elif statement and the non-male/female options as an else
statement. US is pretty big with the LGBTQ+ community when I lived there, so many different people. Keep up the good
work though!
92 words

Permalink Show parent

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 22/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Re: Discussion Unit 3


by Ahmet Yilmazlar - Monday, 29 November 2021, 10:31 AM

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.

elif is an abbreviation of else if. Again, exactly one branch will be executed. There is no limit of the number
of elif statements but only a single (and optional) final else statement is allowed and it must be the last branch in the
statement.

Nesyed conditionals statements mean that you can use one if or else  if statement inside another if or else if statement.

Nested conditionals can become difficult to read, threfore refactoring strategy for avoiding this as it is useful for cleaning up
code or to prepare code for easier extension or reuse

NESTED OUTPUT 
if x == y: print('x and y are equal') else: if x < y: print('x is less than y') else: print('x is greater than y') The outer conditional
contains two branches. The first branch contains a simple statement.

CHAİNED PUTPUT

if x > 0: print('x is positive') The boolean expression after if is called the condition. If it is true, the indented statement runs. If
not, nothing happens.

references
HowtoThink Like a Computer Scientist

Allen Downey

224 words

Permalink Show parent

Re: Discussion Unit 3


by Juan Duran Solorzano - Monday, 29 November 2021, 11:23 AM

Good job, i would like to add some point into your explanation,

A chained conditional is whenever you have to check different conditions at the same time, using ‘elif’ with this elif you
could add as many conditions to be check and perform the action required by them.

Example:

If a > 5:

print(“A is greater than 5”)

elif a == 5: #You can add as many elif you want inside of this chained conditional

print(“A is equal to 5”)

else:

print (“A is less than 5”)

The nested conditional is when you have an if-else inside of an if-else, like this. And only check one condition at the time.


if a > 5:

print(“A is greater than 5”)

else:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 23/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

if a == 5:

print(“Print is equal to 5”)

else:

print(“A is less than 5”)

For your answer I recommend you to, format the text better, as it will be easier for other peers to mark and understand the
program.

Well done and keep it up.


166 words

Permalink Show parent

Re: Discussion Unit 3


by Hyun Oh - Monday, 29 November 2021, 10:46 PM

-Both
chained conditional and nested conditional consist of compound statements
starting with "if" followed by a body of
three or more conditional
possibilities (branches) using "elif" and/or "else" to be
run. However, nested conditional can only be
formed within the body of chained
conditional using its own statement using "if" followed by branches
that contain more
specific or detailed conditions. As nested conditional can
only be formed within the body of chained conditional, it can be
distinguished
by further inside indentation.

-Below are input and output for an example of chained conditional:

-Below are input and output for an example of nested conditional:

-Using
logical operators such as "and", "or", and "not"
comes in handy when avoiding nested conditionals because they can
combine
multiple different conditions into a single condition.

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 24/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

-Below is an example of a nested conditional that can be modified to become a single conditional:

-Below is the equivalent single conditional to the above nested conditional using logical operator "or" and "and":

165 words

Permalink Show parent

Re: Discussion Unit 3


by Yahya Al Hussain - Tuesday, 30 November 2021, 2:00 AM

I like the creative code, the explanation was great and easy to read an understand, including the outputs was a nice touch,
i think i will start doing that as well.
31 words

Permalink Show parent

Re: Discussion Unit 3


by Fouad Tarabay - Tuesday, 30 November 2021, 11:43 AM

Hello Hyun,

You did a great work and the explanation was simple and clear to understand, also using pictures with their discription
make it more easier.
26 words

Permalink Show parent

Re: Discussion Unit 3


by Yahya Al Hussain - Tuesday, 30 November 2021, 1:33 AM

#The difference between a chained conditional and


a nested conditional. 

1* #A chained
conditional is like the name implies, a chain of elif() conditionals that start with an if() statement
and end
with an else() statement which could look like Example 1.

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 25/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Example 1.

if( x=1 ):                                                           #the start of the chain


print("player 1 won the game")

  elif( x=2 ):                                                    


 #an else if() statement

 
  print("player 2 won the game")

 
      elif( x=3 ):                                                 #this
can go on forever

 
        print("player 3 won the game")

 
         else: 
                                                      #closing else:
statement 

 
               print("the game has
ended in a draw")

                                                                          #seeing that this way isn't very efficient, it's better to define a
function
instead, at least for this example.

2*# A nested
conditional is when an if(): statement is used inside another if(): statement.

Example2

if( z=1 ):                                                               #initial if(): statement

print(“player1 has won the game”)

      else:

              if(z=0):                                                  
#nested if(): statement

                 print(“no one has won the


game”)

                    else:

                           if(z=2):

                             print(“player2 has


won the game”)

                                else:

                                      print(“game was a draw”)   

3*#
To help shorten a nested conditional we can use a strategy like the one in Example3.2.

Example3.1

if( z=1 ):                                                             #initial


if(): statement

  if(x=1):                                                           #Nested if(): statement

print(“player1 has won both games”)          #we can do this for all possible combinations

Example3.2

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 26/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

if(z=1 && x=1):                                                #combining both can make it a lot simpler to read even for all possible
combinations       

 print(“player1 has won both games”)

251 words

Permalink Show parent

Re: Discussion Unit 3


by Peter Odetola - Tuesday, 30 November 2021, 5:10 PM

Hello Yahya,

I really commend your efforts in this work. The code was clearly written and I love your comments throughout the
workflow as they give clarity to your steps.
30 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Tuesday, 30 November 2021, 1:50 AM

The difference between these two concepts chained and nested conditionals are in the depth of indentation. For chained
conditionals the use of if/elif/else statements are somewhat like a chain, the depth of indentation is the same for example.

As you can see the depth of indentation is on the same level. Whereas on nested conditionals, like the word suggests the
depth is nested, how nested it can be is up to the code. Here is an example

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 27/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Deeply nested conditionals are very difficult to read, from a Wikipedia contributor (2021) this is what is sometimes called
spaghetti code sometimes even referred to as a big ball of mud when it comes to the overall application making heavy use
of these principles. To avoid this try the following :

Convert your negative checks into positive checks, for example instead of asking if the variable is null rather assume the
variable is positive and the condition will return a bool of true or false.
Replace your conditionals with guards
Allways return values as soon as possible
Decompose your conditionals into small useful functions

References:

Wikipedia contributors. (2021, November 27). Spaghetti code. Wikipedia. https://en.wikipedia.org/wiki/Spaghetti_code

Wikipedia contributors. (2021a, May 19). Big ball of mud. Wikipedia. https://en.wikipedia.org/wiki/Big_ball_of_mud

202 words

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 6:12 AM

Great work using complex and reasonable examples and also pointing out the difference between nested and chained
conditionals clearly. Keep it up.
22 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:52 AM

Thank you Newton I appreciate it


6 words

Permalink Show parent

Re: Discussion Unit 3


by Fouad Tarabay - Tuesday, 30 November 2021, 11:34 AM

Well done, you did a great explanation, it was very clearful to describ your point with the pictures. keep it up!!
21 words

Permalink Show parent

Re: Discussion Unit 3 


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:52 AM

Fouad

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 28/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

I appreciate your kind words, thank you


8 words

Permalink Show parent

Re: Discussion Unit 3


by Peter Odetola - Tuesday, 30 November 2021, 5:29 PM

Hello siphumelelise,

I love the fact that you reference the source of your information for the nested conditionals. You also pointed out in your
explanation and coding that the depth of indentation is on the same level for chained conditionals and nested for nested
conditionals. Kindly review the joining of compound words. some_integer_argument should have been written as
some_Integer_Argument or someIntegerArgument
61 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:51 AM

Thank you Peter, much appreciated


5 words

Permalink Show parent

Re: Discussion Unit 3


by Newton Sudi - Tuesday, 30 November 2021, 5:31 AM

Chained conditionals express more than two branches of computation that have different possibilities. (Downey,2015). I
demonstrated with a simple program regarding chained conditionals statements below.

Program

Input:

x = 2

z = 2

if x = z:

print("system OFF")

elif x == z:

print("system asleep")

else:

print("maintenance needed") #Printing string text, 'maintenance needed' if no value turns true.

Output:

system ON

#From my program above, all conditional statements turns true apart from the last else: statement. But only the first true
conditional statement is executed, thats the reason why the program has only one output.

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 29/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Nested conditionals are a way to nest one conditional statement into another. (Downey,2015).

Demonstration with the program below.

Program

Input:

x = 2

z = 2

if (x==z):

print('System Asleep.😇')

else:

if (xz):

print("System Off.")

else:

print("System needs maintenance.")

Output:

System Asleep.😇

#The first conditional statement nests another conditional statement which has three branches of simple statements which
could be conditional statements if needed.

Although deeply nested conditionals can be complex and difficult to read, they can be simplified using logical operators.
(Downey,2015).

My program below demonstrates how a deeply nested conditional can be simplified using a logical operator.

Program

Input:

#Nested conditionals.

x = 2

z = 2

if (x==z):

print('System Asleep.😇')

else:

if (xz):

print("System Off.")

else:

print("System needs maintenance.")

#Simplified nested conditionals.

x = 0.2

z = 8

if (x<z) and (z<10):

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 30/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

  print("System Asleep.😇")

Output:

System Asleep.😇
System Asleep.😇

#The logical operator "<" in the second #simplified nested conditionals, simplifies the program for easy reading and clear
understanding and produces the same output as the deeply nested conditional statements.

Reference

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

283 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:57 AM

Newton, I enjoyed reading your peace, you demonstrated an understanding of the concepts, keep it up.
16 words

Permalink Show parent

Re: Discussion Unit 3


by Peter Odetola - Tuesday, 30 November 2021, 9:29 AM

CS1102: DISCUSSION UNIT 3

QUESTION 1

Describe the difference between a chained conditional and a nested conditional. Give your own example of each. Do not copy
examples from the textbook.

Chained Conditional

A chained conditional contains a series of alternative branches using if, elif, and else statements that are all indented the same
depth. In a chained conditional, there is no nesting. A chained conditional only executes the first branch that has a true
outcome, no matter how many true conditions are in the chain. It runs through all the conditions one after the other until if
finds the true outcome.
Example of chained conditional

x = 40

y = 130

z = 200

if z > x:

print("z is smaller than a")


elif y < z:
print("y is bigger than z")

elif x < z:

print("x is bigger than z")



elif x < y:

print("x is smaller than y")

else:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 31/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

print("x is smaller than y")

Nested conditional

On the other hand, a nested conditional statement is one in which one conditional statement is nested within another
conditional statement. The depths of the if, elif, and else statements in nested conditionals vary. Therefore, a nested
conditional permits the usage of flow controls, but those conditionals have different depth to create more nuance sequence.

Example of nested conditional

x = 10

y = 10

if x < y:

print("x is smaller than y")

else:

if x > y:

print("x is bigger than y")

else:

print("x is equal to y")

QUESTION 2

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.

Answer: A strategy to avoid nested conditionals is to engage the use of logical operators like “and”, “or”, “not” to create some
flexibility in the statement and simplify them into a single conditional. A typical example is:

if x == y

if y == z

print("x, y and z are the same")

else:

print("x, y and z are not the same")

# A simplified form is shown below:

If x == y and y == z

print("x, y and z are the same")

else:

print("x, y and z are not the same")


388 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:54 AM

Peter,

Great Explanation you demonstrated an understanding of chained and nested conditionals well, well done.
15 words

Permalink Show parent

Re: Discussion Unit 3


by Janelle Bianca Cadawas Marcos - Tuesday, 30 November 2021, 1:10 PM

A chained conditional is a flow of conditions that all have the same


tab depth, while a nested conditional contained
conditionals within
conditionals making it all vary in tab depth.

Chained conditional
example:

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 32/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

>>>if apple
== "red":

. . .       print("Apple will taste sweet.")

. . .   elif apple ==
"green":

. . .      
print("Apple will taste sour.")

. . .   else:

. . .       print("What a unique colored apple!")

Nested conditional
example:

>>>if eyes
== “open”:

. . .       print(“The dog is wide awake.”)

. . .   elif eyes ==


“closed”:

. . .       if body
== “moving”:

. . .            print(“The dog is dreaming.”)

. . .       else:

. . .           
print(“The dog is sleeping soundly.”)

A good strategy to
avoid difficult to read nested conditionals is to try and see if you
can simplify it with logical operators
(Downey, 2015).

Example:

>>>if
days_left == 0:

. . .      
print("It's your birthday!")

. . .   elif
days_left > 0:

. . .       if
days_left < 30:

. . .            print("Your birthday is coming soon!")

. . .       else:

. . .            print("Your birthday is still far.")

Modified example:

>>>if
days_left == 0:

. . .       print("It's your birthday!")



. . .  elif 0 <
days_left < 30:

. . .      
print("Your birthday is coming soon!")

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 33/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

. . .  else:

. . .      
print("Your birthday is still far.")

Reference:

Downey, A. (2015).
Think Python: How to think like a computer scientist. Green Tea
Press. This book is licensed under Creative
Commons
Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0).

258 words

Permalink Show parent

Re: Discussion Unit 3


by Siphumelelise Ntshwenyese - Wednesday, 1 December 2021, 10:59 AM

Janelle,

Well done, you demonstrated an understanding of the concepts, great use of citations, keep it up.
17 words

Permalink Show parent

Re: Discussion Unit 3


by Hamidou Diallo - Wednesday, 1 December 2021, 11:29 PM

Hi Janelle,

I really love your examples. They are just great but make sure to add the starting python block of code on one of your
"else" conditional too. The definition are good too.

Keep up the good work.


39 words

Permalink Show parent

Re: Discussion Unit 3


by Mayowa Ogunyale - Wednesday, 1 December 2021, 5:04 PM

Chained Conditional and Nested Conditional

Main difference is the Nesting

For chained conditional it has several alternatives, but nested conditional is nesting.

Chained conditional

if n+1 < y:

print('n+1 is less than y')

elif n+1 > y:

print('n+1 is greater than y')

else:

print('n+1 and y are equal')

Nested conditional

if (x+1)==(y+1):

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 34/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

print('(x+1) and (y+1) are equal')

else:

if (x+1)<(y+1):

print('(x+1) is less than (y+1)')

else:

print('(x+1) is greater than (y+1)')


71 words

Permalink Show parent

Re: Discussion Unit 3


by Hamidou Diallo - Wednesday, 1 December 2021, 11:24 PM

Hi Mayowa , sorry but I'm not convinced by your definition of the two conditionals. Also your examples.are great but make
sure to starting blocks of code to show that is python code.

Keep up the good work.


38 words

Permalink Show parent

Re: Discussion Unit 3


by Ismail Ahmed - Wednesday, 1 December 2021, 11:47 PM

Hi Mayowa

Great examples. I liked how you referenced too. Keep up the good work.
15 words

Permalink Show parent

Re: Discussion Unit 3


by Hamidou Diallo - Wednesday, 1 December 2021, 7:52 PM

The difference between chained conditionals and nested conditionals

Chained conditionals are conditionals that help programmers to resolve tasks that have multiples possibilities such as
mathematical problems. Generally, they are used when we have more than two possibilities to express like when we do not
have to choose between true or false neither black or white for example, but they might be necessary when we are supposed
to choose one answer within multiple others and this kind of conditionals expression allow us to test each answer till, we find
the right one. While nested conditionals are conditionals expressions that are usually Whitin a first conditional statement (an if
for example) to allow us to explore, treat, or state something when the actual condition is not meet. In some cases, both
chained and nested conditionals can do the same task.

Chained conditional example:

>>> x=y+1

>>> print("equation x=y+1")

>>> import math

>>> if y>0:

print("x is postiive")

>>> elif x<1:

print("x is negative")

>>> else:

print("x is equal to 0")



nested conditional example:
>>> x=10

>>> y= a

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 35/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

>>> print("want to know if x+y is odd or Even")

>>> if a==0:

print( x + y is odd)
>>> else:

>>> if a%2==0:

print("x+y is odd")

>>> else:

print("x+y is even")

how to avoid nested condition:

The the best way to avoid nested conditions is to use logical operators such as AND, OR and NOT(Downey,2015). In fact these
operators can help to simplify conditions and run a program easily.

We want to know the validity of thse square root of ( x - 1 ).

Example1:

sqrt(x-1)?

>>> if x<1:

print("the Expression do not exist")

>>> else:

>>> If x==1:

print("Impossible")

else:

print("the Expression exist")

Example2:

sqrt(x-1)?

if x>1 and x=!0:

print("the Expression exist")

else:

print("the Expression do not exist")

The example 2 helps to realize the same answer with one single condition by using the operator “and”.
315 words

Permalink Show parent

Re: Discussion Unit 3


by Ismail Ahmed - Wednesday, 1 December 2021, 11:47 PM

Hi Hamidou

Great examples. I liked how you referenced too. Keep up the good work.
15 words

Permalink Show parent

Re: Discussion Unit 3


by Crystal Noralez - Wednesday, 1 December 2021, 8:36 PM

Discussion Forum Unit


3

Difference between
a chained conditional and a nested conditional.

 

1.)    Describe
the difference between a chained conditional and a nested conditional. Give
your own example of each.
Do not copy examples from the textbook.

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 36/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Ø 
The
difference between a chained conditional and a nested
conditional is chained conditional is use when there are more
than two
possibilities. More than two branches are needed (Downey, 2015, p.41), and
nested conditional is an “if else” or “if”
statement inside another “if else”
statement, along with that nested conditionals can become difficult to read
very quickly, so it
is a great idea to avoid them when we can (Downey, 2015,
p.42).

2.)    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.

Ø 
Example
of chained conditional and a nested conditional.

chained conditional 

correct =
float(input('enter correct: '))

points=
float (input('enter points:'))

def
Grade(correct,points):

   grade=correct*points

   if (grade < 69.9):

       return 'D, You Failed',grade

   elif (grade >= 70 and grade < 79.9):

       return 'C, You Avarage',grade

   elif (grade >= 80 and grade <89.9):

       return 'B, You Good',grade

   elif (grade >= 90 and grade < 95):

       return 'A, You Excellent',grade

quote,
grade = Grade(correct,points)

print('your
grade is: {} and: {}'.format(grade, quote)) 

nested Conditional

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 37/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

def grade
(fail, pass):

   if fail == true:

       if fail > 69.9:

           print ('you fail it, see you next


year')

       elif 69.9 > fail > 70

       print ('almost there')

       else:

           print ('you can do a make up test')

       else:

           print('put some pride on your


work')

3.
Strategy for avoiding nested conditionals. 

Actually,
I have been sitting here for hours finding bugs on my nested conditionals
example on the question above. The
strategy for avoiding nested conditional is
trying to limit it use as much as possible (Downey, 2015, p.42).

Reference

Downey,
A. (2015). Think Python| How to Think Like a Computer Scientist: Vol.
Version 2.2.23 (2nd Edition). Green Tea Press.
https://my.uopeople.edu/pluginfile.php/1404469/mod_page/content/3/TEXT%20-%20Think%20Python%202e.pdf

349 words
Discussion Forum Unit 3.docx
Permalink Show parent

Re: Discussion Unit 3


by Hamidou Diallo - Wednesday, 1 December 2021, 11:20 PM

Hi Crystal,

Your definition was great, and the comparison too.

However I do not agree with you strategy to avoid nested conditional as sometimes we need to computate complex task.

It's better to use logical operators.

Keep up the good job.


41 words

Permalink Show parent


Re: Discussion Unit 3

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 38/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

by Ismail Ahmed - Wednesday, 1 December 2021, 11:46 PM

Hi Crystal

Great examples. I liked how you referenced too. Keep up the good work.
15 words

Permalink Show parent

Re: Discussion Unit 3


by Ismail Ahmed - Wednesday, 1 December 2021, 11:39 PM

Greetings to our instructor and students

Chained conditional is using multiple else denoted as "elif" which is short term for "else if". Below program demonstrates if
dice rolled selected between 1 to 3 displays the number; else the program will request to roll dice selected again to achieve
result between 1 to 3.

roll_dice=1

if(roll_dice==1):

print("Dice rolled to number 1")

elif(roll_dice==2):

print("Dice rolled to number 2")

elif(roll_dice==3):

print("Dice rolled to number 3")

else:

print("Try to select numbers between 1 to 3")

Nested condition is if else statement where you might find another if and else statement branched on both or any of them(i.e
under if another if else or else under it another if and else statement). Below code has if and else statement below else
condition.

light_mode="Green"

stop_sign="Green" # Try Greens instead

if(stop_sign=="Red"):

print("Red signal stop")

else:

if(stop_sign=="Green"):

print("Green signal stop")

else:

print("Yellow signal stop")

Nested branch can be reduced by using logical operators as demonstrated below

light_mode="Green"

stop_sign="Green" # Try Greens instead


if(stop_sign=="Red"):

print("Red signal stop")

elif(stop_sign=="Green" and light_mode=="Green"):

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 39/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

print("Yellow signal stop")

else:

print("Green signal stop")

Reference:

Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Needham, Massachusetts: Green Tea Press
196 words

Permalink Show parent

UoPeople Clock (GMT-5)

All activities close on Wednesdays at 11:55 PM, except for Learning Journals/Portfolios which close on Thursdays at 11:55 PM always
following the clock at the top of the page.

Due dates/times displayed in activities will vary with your chosen time zone, however you are still bound to the 11:55 PM GMT-5
deadline.

◄ Learning Guide Unit 3

Jump to...

Learning Journal Unit 3 ►

Disclaimer Regarding Use of Course Material  - Terms of Use


University of the People is a 501(c)(3) not for profit organization. Contributions are tax deductible to the extent permitted by law.
Copyright © University of the People 2021. All rights reserved.

You are logged in as Stephen Chuks (Log out)


Reset user tour on this page
 www.uopeople.edu










Resources
UoPeople Library
Orientation Videos
LRC
Syllabus Repository
Honors Lists
Links
About Us
Policies
University Catalog
Support
Student Portal

Faculty
Faculty Portal
CTEL Learning Community

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 40/41
1/5/22, 1:47 AM CS 1101 - AY2022-T2: Discussion Unit 3

Office 365
Tipalti
Contact us
English (‎en)‎
English (‎en)‎
‫ العربية‬‎(ar)‎
Data retention summary
Get the mobile app

https://my.uopeople.edu/mod/forum/discuss.php?d=640871 41/41

You might also like