0% found this document useful (0 votes)
104 views2 pages

CS1101 Unit 3 Discussion

The document describes chained and nested conditionals. A chained conditional uses if/elif/else on the same indentation level while a nested conditional places another conditional within an existing one. An example of each is given. Deeply nested conditionals can be difficult to read. One strategy to avoid nesting is to use logical operators like and/or to combine conditions instead of additional indentation levels. An example converts a nested conditional checking for multiples of 2 and 3 into a single conditional using and.
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)
104 views2 pages

CS1101 Unit 3 Discussion

The document describes chained and nested conditionals. A chained conditional uses if/elif/else on the same indentation level while a nested conditional places another conditional within an existing one. An example of each is given. Deeply nested conditionals can be difficult to read. One strategy to avoid nesting is to use logical operators like and/or to combine conditions instead of additional indentation levels. An example converts a nested conditional checking for multiples of 2 and 3 into a single conditional using and.
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/ 2

I’m using python 3.

10

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

A chained conditional  is basically when you use an if/elif/else flow control and they are all indented on the same level.
They are also useful in situation where a condition can have different outcome/possibility. Whereas, a nested
conditional is where another conditional statement is place within a branch of an already existing conditional statement
which forms a nested shape, i.e. you are placing another if statement in another if statement which can create a much
more complicated computation.

chained conditional example
def discounter(price):
if price < 50:
savings = price * .2
print(f'You are saving ${savings}')
elif price > 50:
savings = price * .15
print(f'You are saving ${savings}')

price = int(input('What is the price of your item '))

discounter(price)

nested conditional example


def treasure_hunt(direction):
if direction == 'right':
answer = input('you came upon a lake, do you swim across or wait')
if answer == "swim":
print("you got eaten by a crocodile, Game Over 😒 ")
elif answer == 'wait':
print(" a boat came and give you a ride ")
choice = input('You came ashore and saw a shovel and a forest. What
is your move? ')
if choice == 'shovel':
print("You dig around and found the treasure 🎊🎊🎊🎊🎊🎊")
else:
print('You went into the jungle and got eaten by a tiger, Game
Over!! ')

else:
print("You met a broken bridge and could not continue your hunt. Game
Over!! ")

direction = input("Choose your paths, Left or Right ").lower()

treasure_hunt(direction)

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.

Response:

One strategy that we can used to avoided nested conditional is to use logical operators (and, or, not) which would
make our code much flexible. Thus, reducing the number of conditional statements needed as can be seen below
Nested conditionals example
num = int(input('enter the number you want to compare '))

if num % 2 == 0:
if num % 3 == 0:
print("This number is a multiple of 2 and 3")
else:
print("this number isnt a multiple of 2 and 3")

Simplified with logical operators

num = int(input('enter the number you want to compare '))

if num % 2 == 0 and num % 3 == 0:


print("This number is a multiple of 2 and 3")
else:
print("this number isnt a multiple of 2 and 3")

References
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea 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