Bird
Raised Fist0
Pythonprogramming~15 mins

Nested conditional execution in Python - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Nested Conditional Execution
📖 Scenario: You are creating a simple program to help a coffee shop decide the price of a coffee order based on the size and whether the customer wants extra syrup.
🎯 Goal: Build a program that uses nested if statements to determine the final price of a coffee order based on size and syrup choice.
📋 What You'll Learn
Create a variable size with exact values: 'small', 'medium', or 'large'
Create a variable extra_syrup with exact boolean values: True or False
Use nested if statements to set the price based on size and extra_syrup
Print the final price as a float with two decimal places
💡 Why This Matters
🌍 Real World
Coffee shops and many businesses use nested conditions to decide prices or options based on multiple choices customers make.
💼 Career
Understanding nested conditions is important for writing clear decision-making code in software development, especially in user input handling and business logic.
Progress0 / 4 steps
1
Set up the coffee order variables
Create a variable called size and set it to the string 'medium'. Create another variable called extra_syrup and set it to True.
Python
Hint

Use = to assign values. Strings need quotes, booleans are True or False.

2
Create a base price variable
Create a variable called price and set it to 0.0 as a starting point for the coffee price.
Python
Hint

Start with a price of zero before deciding the final price.

3
Use nested if statements to set the price
Use nested if statements with size and extra_syrup to set price as follows:
- If size is 'small', price is 2.0, add 0.5 if extra_syrup is True.
- If size is 'medium', price is 3.0, add 0.5 if extra_syrup is True.
- If size is 'large', price is 4.0, add 0.5 if extra_syrup is True.
Python
Hint

Check the size first, then inside that check if extra_syrup is True to add 0.5.

4
Print the final price
Print the price variable formatted to two decimal places using print(f"{price:.2f}").
Python
Hint

Use an f-string to format the price with two decimals.

Practice

(1/5)
1. What does nested conditional execution mean in Python?
easy
A. An if or else inside another if or else
B. Using multiple if statements one after another
C. Writing if statements without indentation
D. Using only one if statement in a program

Solution

  1. Step 1: Understand the meaning of nested conditionals

    Nested conditionals mean putting one conditional inside another, like an if inside an if.
  2. Step 2: Compare options to definition

    An if or else inside another if or else correctly describes this as an if or else inside another if or else. Other options describe different or incorrect ideas.
  3. Final Answer:

    An if or else inside another if or else -> Option A
  4. Quick Check:

    Nested conditional = if inside if [OK]
Hint: Look for conditionals inside other conditionals [OK]
Common Mistakes:
  • Thinking multiple separate ifs are nested
  • Ignoring indentation importance
  • Confusing nested with chained conditionals
2. Which of the following is the correct syntax for nested conditionals in Python?
easy
A. if x > 0: if x < 10: print('x is between 1 and 9')
B. if x > 0 if x < 10: print('x is between 1 and 9')
C. if x > 0: if x < 10: print('x is between 1 and 9')
D. if x > 0: print('x is positive') else if x < 10: print('x is less than 10')

Solution

  1. Step 1: Check indentation and colons

    Python requires colons after if and proper indentation for nested blocks.
  2. Step 2: Analyze each option

    if x > 0: if x < 10: print('x is between 1 and 9') uses colons and indents the inner if correctly. Options A and B miss colons or indentation. if x > 0: print('x is positive') else if x < 10: print('x is less than 10') uses invalid else if instead of elif.
  3. Final Answer:

    if x > 0: if x < 10: print('x is between 1 and 9') -> Option C
  4. Quick Check:

    Colons + indentation = correct syntax [OK]
Hint: Check colons and indentation for nested blocks [OK]
Common Mistakes:
  • Missing colons after if statements
  • Incorrect indentation of nested if
  • Using else if instead of elif
3. What is the output of this code?
score = 85
if score >= 90:
    print('Grade A')
else:
    if score >= 80:
        print('Grade B')
    else:
        print('Grade C')
medium
A. Grade A
B. Grade B
C. Grade C
D. No output

Solution

  1. Step 1: Check first condition

    score is 85, which is not >= 90, so skip first print.
  2. Step 2: Check nested else condition

    Inside else, check if score >= 80. 85 >= 80 is True, so print 'Grade B'.
  3. Final Answer:

    Grade B -> Option B
  4. Quick Check:

    85 >= 80 triggers nested if [OK]
Hint: Follow conditions step-by-step inside else [OK]
Common Mistakes:
  • Assuming first if is true for 85
  • Ignoring nested else block
  • Confusing indentation levels
4. Find the error in this nested conditional code:
num = 5
if num > 0:
if num < 10:
print('Number is between 1 and 9')
medium
A. Missing colon after first if
B. No error, code is correct
C. Using print without parentheses
D. Incorrect indentation of inner if and print

Solution

  1. Step 1: Check colons

    Both if statements have colons, so no missing colon error.
  2. Step 2: Check indentation

    Inner if and print are not indented under outer if, causing syntax error.
  3. Final Answer:

    Incorrect indentation of inner if and print -> Option D
  4. Quick Check:

    Nested blocks must be indented [OK]
Hint: Indent nested if and its code properly [OK]
Common Mistakes:
  • Forgetting to indent nested blocks
  • Confusing missing colon with indentation error
  • Assuming print syntax error without checking Python version
5. You want to check if a number is positive, and if positive, check if it is even or odd. Which nested conditional code correctly prints 'Positive even' or 'Positive odd' accordingly?
hard
A. if num > 0: if num % 2 == 0: print('Positive even') else: print('Positive odd')
B. if num > 0 and num % 2 == 0: print('Positive even') else: print('Positive odd')
C. if num > 0: if num % 2 == 0: print('Positive even') else: print('Positive odd')
D. if num > 0: if num % 2 == 0: print('Positive even') else: print('Positive odd')

Solution

  1. Step 1: Understand the logic needed

    First check if number is positive, then inside that check if even or odd.
  2. Step 2: Analyze options for correct nesting and indentation

    if num > 0: if num % 2 == 0: print('Positive even') else: print('Positive odd') correctly nests the even/odd check inside the positive check with proper indentation. if num > 0 and num % 2 == 0: print('Positive even') else: print('Positive odd') combines conditions but does not handle odd positive numbers correctly. Options C and D have indentation errors.
  3. Final Answer:

    if num > 0: if num % 2 == 0: print('Positive even') else: print('Positive odd') -> Option A
  4. Quick Check:

    Nested if with correct indentation prints right message [OK]
Hint: Indent inner if inside outer if for stepwise checks [OK]
Common Mistakes:
  • Combining conditions incorrectly
  • Indentation errors in nested blocks
  • Missing else for odd case