Programming fundamentalsLesson 1 of 1 in the course Decisions

Python Nested Decisions: Branches, Indentation, and Execution Paths

5 flashcards · 5 quiz questions · 5 match pairs · 4 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Nested decisions let a program make a second choice only after it has made an initial choice. This is useful when the rules for one decision depend on the result of another, such as choosing a menu before selecting a meal.

A nested decision is an if, elif, or else statement placed inside the body of another decision statement. The inner decision is reached only when execution enters the outer branch that contains it.

All flashcards

What is a nested decision?
A conditional statement inside another conditional statement. The outer decision selects a branch, and a condition inside that branch makes a more specific choice.
How does indentation affect nested decisions in Python?
Indentation shows which statements belong to each branch. Inner decisions are indented farther than the outer decision, and changing indentation can change which `if` an `else` belongs to.
How should you trace a nested decision?
Evaluate the outer condition first, enter the selected branch, and then evaluate the conditions nested inside it. Run the first matching inner branch, or its `else` if none match.
Why use nested decisions when checking game player counts?
Different games can have different valid player ranges. Checking the game first lets the program apply the correct player-count rules.
How does a menu-selection program handle an unknown menu or a meal number outside 1–3?
It assigns `None` to the choice and prints an invalid-choice message.

Quiz questions

  1. What is a nested decision?Answer: A conditional statement placed inside another conditional statement.
  2. What does indentation indicate in Python nested decisions?Answer: Which statements belong to each conditional branch.
  3. A program has different valid player-count ranges for different games. Why should it check the game before checking the player count?Answer: The selected game determines which player-count rules apply.
  4. A game allows 2–4 players. In a nested decision that checks whether the count is below 2, then above 4, what is printed for 5 players?Answer: Too many players
  5. A menu maps lunch meal 1 to Caesar salad, 2 to a chicken wrap, and 3 to squash soup. What is selected for lunch meal 3?Answer: Butternut squash soup is selected and printed.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons