Skip to the content.

Big Idea 3 Unit 6

Big Idea 3 Unit 6

Oct 11, 2024 • 1 min read


Summary

Nested conditionals are conditionals placed inside other conditionals. They help handle multiple layers of decision-making when there are several possible scenarios.


Example 1: Choosing an iPhone Based on Budget

budget = 1100  

iphone_15_price = 1204
iphone_12_price = 1099
iphone_10_price = 807

if budget >= iphone_15_price:
    print("You can buy an iPhone 15!")
elif budget >= iphone_12_price:
    print("You can buy an iPhone 12!")
elif budget >= iphone_10_price:
    print("You can buy an iPhone 10!")
else:
    print("You don't have enough money to buy an iPhone.")



```python
has_flour = True
has_sugar = True
has_eggs = True
has_milk = False
has_baking_powder = True
has_vanilla_extract = True

if has_flour and has_sugar:
    print("You have the basic ingredients.")
    
    if has_eggs:
        print("You can make a basic cake batter.")

        if has_milk:
            print("Great! The cake will be moist.")
        else:
            print("You don't have milk, the cake might be a bit dry.")
        
        if has_baking_powder:
            print("Your cake will rise perfectly.")
        else:
            print("Without baking powder, the cake might not rise.")
        
        if has_vanilla_extract:
            print("The cake will have a nice vanilla flavor.")
        else:
            print("No vanilla extract, the flavor will be more basic.")
    else:
        print("You can't bake a cake without eggs.")
else:
    print("You don't have enough ingredients to bake a cake.")

hot = False
mid = True
cold = True
foggy = False
rainy = True
thunderstormy = False

if not hot and mid:
    print("It's decent weather.")
    
    if cold:
        print("Cold weather is better weather")

        if foggy:
            print("Foggy weather is great weather")
        else:
            print("Unfortunately it's not foggy")
        
        if rainy:
            print("Rainy weather is wonderful weather! Be happy.")
        else:
            print("Unfortunately, it's not rainy")
        
        if thunderstormy:
            print("It's thunderstormy!! Thunderstorms are so fun.")
        else:
            print("Aww, it's not thunderstormy.")
    else:
        print("Aww it's just meh weather")
else:
    print("Bad weather. Bad bad bad bad weather.")