![]() |
Home | Accounts | Setup | Verify | Play | Hacks |
3.6 Conditinals all Hacks
All hacks for Conditionals
Popcorn Hack #1
Write a general conditional based on a topic of you choice Use an if and an else statement
# Define a temperature in degrees
temperature = 25
# Check the temperature category
if temperature > 30:
print("It's hot outside, stay cool!")
elif temperature >= 15:
print("It's warm outside, enjoy the weather!")
else:
print("It's cold outside, dress warmly!")
It's warm outside, enjoy the weather!
%%js
// Define a temperature in degrees
const temperature = 25;
// Check the temperature category
if (temperature > 30) {
console.log("It's hot outside, stay cool!");
} else if (temperature >= 15) {
console.log("It's warm outside, enjoy the weather!");
} else {
console.log("It's cold outside, dress warmly!");
}
<IPython.core.display.Javascript object>
Popcorn hack #2
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
# Ask the user if they are a student to determine discount eligibility
is_student = input("Are you a student? (yes/no): ").strip().lower() == "yes"
# Check the boolean variable
if is_student:
print("You are eligible for a student discount!")
else:
print("You are not eligible for a student discount.")
You are eligible for a student discount!
%%js
// Ask the user if they are a student to determine discount eligibility
const isStudent = prompt("Are you a student? (yes/no): ").trim().toLowerCase() === "yes";
// Check the boolean variable
if (isStudent) {
console.log("You are eligible for a student discount!");
} else {
console.log("You are not eligible for a student discount.");
}
<IPython.core.display.Javascript object>
Popcorn Hack #3
import datetime
# Get the current day of the week
current_day = datetime.datetime.today().weekday()
# Check if it's the weekend (Saturday or Sunday)
if current_day >= 5:
print("It's the weekend! Time to relax.")
else:
print("It's a weekday. Keep up the hard work!")
%%js
// Get the current day of the week
const today = new Date();
const currentDay = today.getDay(); // 0 for Sunday, 6 for Saturday
// Check if it's the weekend (Saturday or Sunday)
if (currentDay === 0 || currentDay === 6) {
console.log("It's the weekend! Time to relax.");
} else {
console.log("It's a weekday. Keep up the hard work!");
}
Homework hack #1
import sys
def question_with_response(prompt):
answer = input(prompt)
return answer
questions = 5
correct = 0
user_name = question_with_response("Enter your name: ")
print(f'Hello, {user_name}! You are running Python using {sys.executable}.')
ready = question_with_response("Are you ready to take a general knowledge quiz? (yes/no): ")
if ready.lower() != 'yes':
print("Come back when you're ready! Goodbye.")
sys.exit()
q1 = question_with_response("1. What is the capital of France? (a) Berlin (b) Madrid (c) Paris: ")
if q1.lower() == "c":
print("Correct!")
correct += 1
else:
print("Incorrect. The correct answer is (c) Paris.")
q2 = question_with_response("2. Who wrote 'Romeo and Juliet'? (a) Mark Twain (b) William Shakespeare (c) Jane Austen: ")
if q2.lower() == "b":
print("Correct!")
correct += 1
else:
print("Incorrect. The correct answer is (b) William Shakespeare.")
q3 = question_with_response("3. What is the largest planet in our solar system? (a) Earth (b) Jupiter (c) Saturn: ")
if q3.lower() == "b":
print("Correct!")
correct += 1
else:
print("Incorrect. The correct answer is (b) Jupiter.")
q4 = question_with_response("4. In which year did the Titanic sink? (a) 1912 (b) 1914 (c) 1918: ")
if q4.lower() == "a":
print("Correct!")
correct += 1
else:
print("Incorrect. The correct answer is (a) 1912.")
q5 = question_with_response("5. What is the chemical symbol for gold? (a) Au (b) Ag (c) Fe: ")
if q5.lower() == "a":
print("Correct!")
correct += 1
else:
print("Incorrect. The correct answer is (a) Au.")
print(f'{user_name}, you scored {correct}/{questions}!')
print("\nHomework Hack: Let's try a simple comparison program!")
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
if num1 == num2:
print("Both numbers are equal.")
elif num1 > num2:
print(f"The first number ({num1}) is larger.")
else:
print(f"The second number ({num2}) is larger.")