Fall 2024 - P2
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.6-P2 Conditionals
Student led teaching on Conditionals using Javascript and Python
Content Outline
3.6.1
- If statements
- Else statements
- Javascript and Python examples
3.6.2
- Examples with Boolean
- Usage of modules
- Homework Assignment(Example Included)
Popcorn Hack #1
Task:
- Write a general conditional based on a topic of you choice
- Use an if and an else statement
score = 85
# Conditional statement
if score >= 75:
print("You passed the test!")
else:
print("You failed the test.")
<IPython.core.display.Javascript object>
%%js
let score = 85;
// Conditional statement
if (score >= 75) {
console.log("You passed the test!");
} else {
console.log("You failed the test.");
}
<IPython.core.display.Javascript object>
Popcorn Hack #2
Criteria:
- Use a boolean value to check in a conditional
- Set an original value for the boolean variable as an input
- Create a conditional using said boolean variable checking if its true or false.
- Complete in Javascript and Python
owns_a_key = True
if owns_a_key:
print("Come on in!")
else:
print("You need a key to come in here")
%%js
let owns_a_key = true;
if (owns_a_key) {
console.log("Come on in!");
} else {
console.log("You need a key to come in here.");
}
<IPython.core.display.Javascript object>
Popcorn Hack #3
Task
Use a library of your choice to practice your skills with conditionals
Criteria:
- Import any kind of library.
- Use a conditional using the library you imported(be creative with how you use it)
- Complete in Javascript and Python
import random
roll_dice = random.randint(1, 6)
print("Rolling the dice...")
print(f"You rolled a {roll_dice}")
if roll_dice == 1:
print("Oh no! You rolled a one. Try again!")
elif roll_dice <= 3:
print("You rolled a low number. Better luck next time!")
elif roll_dice == 4:
print("Not bad! You rolled a four.")
elif roll_dice <= 5:
print("Great roll! You rolled a five.")
else:
print("Awesome! You rolled a six! You're on a roll!")
%%js
let roll_dice = Math.floor(Math.random() * 6) + 1;
console.log("Rolling the dice...");
console.log(`You rolled a ${roll_dice}`);
if (roll_dice === 1) {
console.log("Oh no! You rolled a one. Try again!");
} else if (roll_dice <= 3) {
console.log("You rolled a low number. Better luck next time!");
} else if (roll_dice === 4) {
console.log("Not bad! You rolled a four.");
} else if (roll_dice === 5) {
console.log("Great roll! You rolled a five.");
} else {
console.log("Awesome! You rolled a six! You're on a roll!");
}
<IPython.core.display.Javascript object>