Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
CSP Period 4 Unit 3.6.4 Conditionals in Python Hacks
In this lesson, students will practice using if-else statements in Python through a series of hands-on exercises. They will solve problems that require them to make decisions in their code based on different conditions. These exercises will cover checking conditions with if, handling multiple outcomes with elif, and providing fallback options with else. Students will also practice combining conditions using and, or, and not to create more complex decision-making programs. By the end of the lesson, students will be more confident in using conditionals to solve real-world problems.
Hack 1: Odd or Even Checker
Description: This hack checks if a given number is odd or even.
- Define a function named
check_odd_even
that takes one parameter:number
. - Use an
if
statement to check if the number is divisible by 2. - Return
"Even"
if true; otherwise, return"Odd"
.
Call the function with different numbers to test your work.
Hack 2: Leap Year Checker
Description: This hack determines if a given year is a leap year.
- Define a function named
is_leap_year
that takes one parameter:year
. - Use an
if
statement to check if the year is divisible by 4 but not by 100, or divisible by 400. - Return
"Leap Year"
if true; otherwise, return"Not a Leap Year"
.
Call the function with different years to test your work.
Hack 3: Temperature Range Checker
Description: This hack checks if a given temperature is considered cold, warm, or hot.
- Define a function named
temperature_range
that takes one parameter:temperature
. - Use
if...elif...else
statements to categorize the temperature:- Return
"Cold"
for temperatures below 60°F. - Return
"Warm"
for temperatures between 60°F and 80°F. - Return
"Hot"
for temperatures above 85°F.
- Return
Call the function with different temperature values to check your work.