Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
CSP Period 4 Unit 3.7.3 Nested Conditionals Python Hacks
In this lesson on nested conditionals in Python, students will explore how to create decision-making structures that involve multiple layers of conditions. By learning how to nest if, elif, and else statements, they will be able to write more precise and complex programs where one condition is checked only after another condition is met. The lesson will include hands-on exercises to help students understand when and how to apply nested conditionals for better control flow and logical clarity in their code.
Python Nested Conditionals Hacks Below
Meet the following conditions:
The weather must be sunny.
You must have sunscreen.
You must have enough snacks.
If all conditions are met, output a message that says you are ready for the beach.
If you don’t have enough snacks, suggest getting snacks first.
If you don’t have sunscreen, suggest buying sunscreen.
If the weather is not sunny, output that it’s not a good day for the beach.
Include the following conditions:
You must be 18 years or older.
You must have enough space in your home (more than 50 square feet).
You must be available to take care of the pet (true/false).
If all conditions are true, print that you can adopt the pet.
If you don’t have enough space, print that you need a bigger home.
If you aren’t available to take care of the pet, print that you need to make time.
If you are not 18 or older, print that you must be at least 18 to adopt a pet.
Check the following conditions:
The weather must be clear.
You must have running shoes.
You must have practiced for at least 10 days.
If all conditions are met, print that you are ready for the marathon.
If you don’t have running shoes, print that you need to buy shoes first.
If you haven’t practiced enough, print that you need to practice more.
If the weather is not clear, print that it’s not the right time for the marathon.
Python Quiz
Question 1:
What will be the output of the following code?
let age = 16;
let hasPermission = false;
if (age >= 18) {
if (hasPermission) {
console.log("You can enter.");
} else {
console.log("You need permission to enter.");
}
} else {
console.log("You are too young to enter.");
}
A. You can enter. B. You need permission to enter. C. You are too young to enter.
Question 2:
Given the following code, what will be printed if x = 10 and y = -5?
let x = 10;
let y = -5;
if (x > 0) {
if (y > 0) {
console.log("Both x and y are positive.");
} else {
console.log("x is positive but y is not.");
}
} else {
console.log("x is not positive.");
}
A. Both x and y are positive. B. x is positive but y is not. C. x is not positive.
Question 3:
What will be the output of the following code?
let temperature = 65;
let isSunny = true;
if (temperature > 70) {
if (isSunny) {
console.log("It's a nice day for a picnic.");
} else {
console.log("It's warm but not sunny.");
}
} else {
console.log("It's too cold.");
}
A. It’s a nice day for a picnic. B. It’s warm but not sunny. C. It’s too cold.