4.6: Nested Decisions
- Page ID
- 117545
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
By the end of this section you should be able to
- Describe the execution paths of programs with nested
if-elsestatements. - Implement a program with nested
if-elsestatements.
Nested decision statements
Suppose a programmer is writing a program that reads in a game ID and player count and prints whether the user has the right number of players for the game.
The programmer may start with:
if game == 1 and players < 2:
print("Not enough players")
if game == 1 and players > 4:
print("Too many players")
if game == 1 and (2 <= players <= 4):
print("Ready to start")
if game == 2 and players < 3:
print("Not enough players")
if game == 2 and players > 6:
print("Too many players")
if game == 2 and (3 <= players <= 6):
print("Ready to start")
The programmer realizes the code is redundant. What if the programmer could decide the game ID first and then make a decision about players? Nesting allows a decision statement to be inside another decision statement, and is indicated by an indentation level.
An improved program:
if game == 1:
if players < 2:
print("Not enough players")
elif players > 4:
print("Too many players")
else:
print("Ready to start")
if game == 2:
if players < 3:
print("Not enough players")
elif players > 6:
print("Too many players")
else:
print("Ready to start")
# Test game IDs 3-end
Consider the example above. Given leaf_count = 9 and leaf_shape = "teardrop", what is the output?
Might be poison ivyMight be poison oakMight be poison sumac
- Answer
-
c.
leaf_count == 3isFalse, so the outerelse 's body is executed.
Given num_dancers = 49, what is printed?
if num_dancers < 0:
print("Error: num_dancers is negative")
else:
if num_dancers % 2 == 1:
print("Error: num_dancers is odd")
print(num_dancers, "dancers")
Error: num_dancers is odd49 dancersError: num_dancers is odd
49 dancers
- Answer
-
c.
num_dancersis positive, so theelsebranch is taken.num_dancersis odd, so the error is printed before the program continues after the nestedifand printsnum_dancers.
Given x = 256, y = 513, and max = 512, which of the following will execute?
if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5
Body 2Body 2,Body 3Body 2,Body 5
- Answer
-
b.
256 == 513isFalse, and256 < 513isTrue, so theelifbranch is taken andBody 2executes.513 >= 512isTrue, soBody 3executes.
Given x =118, y = 300, and max = 512, which of the following will execute?
if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5
Body 2Body 3Error
- Answer
-
c. The first
elseisn't indented and is treated as theelseto the originalif. Thus the secondelseis not connected to anifstatement and produces an error.
Write a program that reads in a string, "lunch" or "dinner", representing the menu choice, and an integer, 1, 2, or 3, representing the user's meal choice. The program then prints the user's meal choice.
Lunch Meal Options
- 1: Caesar salad
- 2: Spicy chicken wrap
- 3: Butternut squash soup
Dinner Meal Options
- 1: Baked salmon
- 2: Turkey burger
- 3: Mushroom risotto
Ex: If the input is:
lunch
3
The output is:
Your order: Butternut squash soup
Interactive Code
- Input
-
dinner
1 - Answer
-
menu_choice = input()
meal_choice = int(input())
if menu_choice == "lunch":
if meal_choice == 1:
order = "Caesar salad"
elif meal_choice == 2:
order = "Spicy chicken wrap"
else:
order = "Butternut squash soup"
else:
if meal_choice == 1:
order = "Baked salmon"
elif meal_choice == 2:
order = "Turkey burger"
else:
order = "Mushroom risotto"print("Your order:", order)


