Hack 1, 3.6.3: Conditionals
# Example of Python Conditionals with comments
# Step 1: Set an example value for age
age = 18
# Step 2: Use an if statement to check if the age is less than 18
if age < 18:
# This block runs if the condition is True
print("You are a minor.")
# Step 3: Use an elif statement to check if age equals 18
elif age == 18:
# This block runs if the age is exactly 18
print("You just became an adult!")
# Step 4: Use an else statement for any other cases (age greater than 18)
else:
# This block runs if none of the previous conditions were True
print("You are an adult.")
You just became an adult!
Hack 2, 3.6.2: Js conditional
// Function to get a grade based on score
function getGrade() {
// Prompt the user to input a score
let score = prompt("Enter the score (0-100):");
score = Number(score); // Convert input to a number
// Check if the input is a valid number
if (isNaN(score) || score < 0 || score > 100) {
console.log("Please enter a valid score between 0 and 100.");
} else {
// Check the score using conditionals
if (score >= 90) {
console.log("A"); // Grade A for scores 90 and above
} else if (score >= 80) {
console.log("B"); // Grade B for scores between 80 and 89
} else if (score >= 70) {
console.log("C"); // Grade C for scores between 70 and 79
} else {
console.log("F"); // Grade F for scores below 70
}
}
}
// Call the function to get a grade
getGrade();
Cell In[12], line 1
// Function to get a grade based on score
^
SyntaxError: invalid syntax
Hack 3, 3.7.2: PYTHON popcorn
# Function to determine the grade based on a score
def determine_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
# Nested conditionals for borderline cases
if score >= 60:
print("Borderline case: Checking additional criteria...")
extra_credit = input("Did the student complete extra credit? (yes/no): ").lower()
if extra_credit == "yes":
return "C"
else:
return "D"
else:
return "F"
# Get user input for the score
try:
score = int(input("Enter the student's score (0-100): "))
if 0 <= score <= 100:
grade = determine_grade(score)
print(f"The student's grade is: {grade}")
else:
print("Please enter a valid score between 0 and 100.")
except ValueError:
print("Invalid input! Please enter a numeric value.")
Popcorn Hack 4, 3.7.3: js
// Function to assess fitness level based on number of push-ups
function assessFitnessLevel(pushUps) {
let fitnessLevel;
if (pushUps >= 50) {
fitnessLevel = "Elite";
} else {
if (pushUps >= 30) {
fitnessLevel = "Advanced";
} else {
if (pushUps >= 15) {
fitnessLevel = "Intermediate";
} else {
if (pushUps >= 1) {
fitnessLevel = "Beginner";
} else {
fitnessLevel = "Unfit";
}
}
}
}
return fitnessLevel;
}
// Example usage
const userPushUps = 28; // You can change this value to test different inputs
console.log(`Fitness Level: ${assessFitnessLevel(userPushUps)}`);