You are on page 1of 3

Discussion Forum 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.
Answer.
A chained conditional is a conditional statement with a series of alternative branches while a
nested conditional appears in one of the branches of another conditional statement. Examples are
as follows;
Example of a chained conditional
>>> # The following program displays the winner of a football match after the following
variables have been input:
H=3 # home team regular time A=3 # away team regular time H2=0 # home team extra time(in case of a draw in regular time) A2=3 # away team extra time(in case of a draw in regular time) P1=0 # home team penalty

shootout P2=2 # away team penalty shootout

if H > A:
print('Home team Wins!')
elif H2 > A2:
print('Home team wins after extra time!')
elif P1 > P2:
print('Home team wins on penalties')
elif H < A:
print('Away team Wins!')
elif H2 < A2:
print('Away team wins after extra time') elif P1 < P2:
print('Away team wins on penalties') elif H==A: print('Extra time!')
else:
print('Match Cancelled!')
The output of the above program is as follows:
Away team wins after extra time
>>>
Example of a nested conditional.
# This program displays an online procedure required to purchase a video game using credits and
money.
points=3000 # credits acquired in the gaming platform cash=300 # money in the user's gaming
account.
if points>1000:
print('Your points plus $50 will buy the Game') if cash>=50:

print('Purchase approved!')
else:
print('You need to deposit money into your account')
else:
print('Purchase impossible') # by changing the variables (points and money) one gets different
output statements.
the output of the program above is;
Your points plus $50 will buy the Game Purchase approved!
>>>

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.
Ans.
Logical operators can be used to simplify a nested conditional into a single conditional. In the
example below I have used the ‘and’ operator to simplify the nested conditional.
Example.
 # This program displays an online procedure required to purchase a video game using credits
and money.
points=3000 # credits acquired in the gaming platform
cash=300 # money in the user's gaming account.
if points>1000 and cash>=50:
print('Your points plus $50 will buy the Game, Purchase approved!')
else:
print('Check your credits or cash. Purchase impossible') # by changing the variables (points and
money) one gets different output statements.
The output of the above program is:
Your points plus $50 will buy the Game, Purchase approved! >>>
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
BYNC 3.0).

You might also like