Homeworks and Popcorn Hacks for Lesson 3.6/3.7
All homeworks for Lessons
3.6 and 3.7 Popcorn Hacks
Popcorn Hack 1
def is_even_or_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
print(is_even_or_odd(4))
print(is_even_or_odd(7))
print(is_even_or_odd(0))
print(is_even_or_odd(-3))
Even
Odd
Even
Odd
Popcorn Hack 2
def is_even_or_odd(boolean_value):
if boolean_value == 1:
return "Odd"
elif boolean_value == 0:
return "Even"
else:
return "Invalid input: Please provide 0 or 1."
print(is_even_or_odd(1))
print(is_even_or_odd(0))
print(is_even_or_odd(2))
Odd
Even
Invalid input: Please provide 0 or 1.
Popcorn Hack 3
person = {
"name": "Nolan",
"age": 24,
"profession": "Student",
"is_student": True
}
def check_student_status(person):
if person["is_student"]:
return f"{person['name']} is a student."
else:
return f"{person['name']} is not a student."
print(check_student_status(person))
Nolan is a student.
Popcorn Hack 4
def popcorn_size_message(size):
if size == "small":
return "You ordered a small popcorn. Enjoy!"
elif size == "medium":
return "You ordered a medium popcorn. Now go."
elif size == "large":
return "You ordered a large popcorn. Get ready for a big treat big boy!"
else:
return "Invalid size."
print(popcorn_size_message("small"))
print(popcorn_size_message("medium"))
print(popcorn_size_message("large"))
print(popcorn_size_message("extra large"))
You ordered a small popcorn. Enjoy!
You ordered a medium popcorn. Now go.
You ordered a large popcorn. Get ready for a big treat big boy!
Invalid size.
Popcorn Hack 5
%%js
function suggestOutdoorActivity(weatherCondition) {
if (weatherCondition === "sunny") {
return "It's sunny! How about going for a hike or enjoying a picnic in the park?";
} else if (weatherCondition === "raining") {
return "It's raining! Maybe it's a good time to stay indoors and read a book or watch a movie.";
} else {
return "Invalid weather condition. Please enter 'sunny' or 'raining'.";
}
}
console.log(suggestOutdoorActivity("sunny"));
console.log(suggestOutdoorActivity("raining"));
console.log(suggestOutdoorActivity("cloudy"));
<IPython.core.display.Javascript object>
Popcorn Hack 6
def check_affordable_laptops(savings, laptop_prices):
affordable_laptops = []
for laptop, price in laptop_prices.items():
if savings >= price:
affordable_laptops.append(laptop)
if affordable_laptops:
return f"You can afford the following laptops: {', '.join(affordable_laptops)}"
else:
return "Sorry, you cannot afford any of these laptops at the moment."
savings = float(input("Enter your savings: $"))
laptop_prices = {
"Laptop A": 2400,
"Laptop B": 1500,
"Laptop C": 750,
"Laptop D": 1275
}
print(check_affordable_laptops(savings, laptop_prices))
You can afford the following laptops: Laptop B, Laptop C, Laptop D
Homework Hack 1
def ask_question(question, options, correct_answer):
print(question)
for i, option in enumerate(options, 1):
print(f"{i}. {option}")
try:
answer = int(input("Your answer (1-4): "))
if options[answer - 1] == correct_answer:
print("Correct!\n")
return 1
else:
print(f"Wrong! The correct answer was: {correct_answer}\n")
return 0
except (ValueError, IndexError):
print("Invalid input. Please select a number between 1 and 4.\n")
return 0
def quiz_game():
score = 0
questions = [
{
"question": "What is the capital of France?",
"options": ["Berlin", "Madrid", "Paris", "Rome"],
"correct_answer": "Paris"
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["Earth", "Mars", "Venus", "Jupiter"],
"correct_answer": "Mars"
},
{
"question": "Who wrote the play 'Romeo and Juliet'?",
"options": ["William Shakespeare", "Charles Dickens", "Jane Austen", "George Orwell"],
"correct_answer": "William Shakespeare"
},
{
"question": "What is the largest mammal?",
"options": ["Elephant", "Blue Whale", "Giraffe", "Shark"],
"correct_answer": "Blue Whale"
},
{
"question": "What is the boiling point of water in Celsius?",
"options": ["90°C", "100°C", "120°C", "200°C"],
"correct_answer": "100°C"
}
]
for q in questions:
score += ask_question(q["question"], q["options"], q["correct_answer"])
print(f"Your final score is {score}/{len(questions)}.")
quiz_game()
What is the capital of France?
1. Berlin
2. Madrid
3. Paris
4. Rome
Wrong! The correct answer was: Paris
Which planet is known as the Red Planet?
1. Earth
2. Mars
3. Venus
4. Jupiter
Wrong! The correct answer was: Mars
Who wrote the play 'Romeo and Juliet'?
1. William Shakespeare
2. Charles Dickens
3. Jane Austen
4. George Orwell
Wrong! The correct answer was: William Shakespeare
What is the largest mammal?
1. Elephant
2. Blue Whale
3. Giraffe
4. Shark
Correct!
What is the boiling point of water in Celsius?
1. 90°C
2. 100°C
3. 120°C
4. 200°C
Wrong! The correct answer was: 100°C
Your final score is 1/5.
Homework Hack 2
def check_eligibility():
age = int(input("Please enter your age: "))
has_ball = input("Do you have a ball (yes/no)? ").strip().lower()
if age >= 5 and has_ball == "yes":
if age < 8:
group = "under 15"
else:
group = "15 and older"
print(f"Great! You can join the game in the '{group}' group.")
else:
print("Sorry, you are not eligible to join the game. You must be at least 15 years old and have a ball.")
check_eligibility()
Great! You can join the game in the '15 and older' group.