Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
Solutions for Hacks!
Warning! Has answers to the best practice problems.
Answer to Popcorn Hack 1:
Warning! Has answers to best practice problems.
# Ticket pricing system
base_ticket_price = 10.00
# Get the user's age
age = int(input("Please enter your age: "))
# Determine ticket price based on age
if age <= 12:
ticket_price = base_ticket_price * 0.5 # 50% off for children
print(f"Child ticket price: ${ticket_price:.2f}")
elif age <= 63:
ticket_price = base_ticket_price # Full price for adults
print(f"Adult ticket price: ${ticket_price:.2f}")
else:
ticket_price = base_ticket_price * 0.7 # 30% off for seniors
print(f"Senior ticket price: ${ticket_price:.2f}")
# Check if the user has a ticket
has_ticket = input("Do you have a ticket? (yes/no): ").lower() == "yes"
if has_ticket:
# Check if the user is a student
while True:
is_student = input("Are you a student? (yes/no): ").lower()
if is_student == "yes":
student_discount = 0.20 # 20% discount
final_price = ticket_price * (1 - student_discount)
print(f"You are eligible for a student discount! The ticket price is now: ${final_price:.2f}")
break
elif is_student == "no":
print(f"No student discount applied. The ticket price is: ${ticket_price:.2f}")
break
else:
print('Invalid response. Please answer with "yes" or "no".')
else:
print("You need a ticket to enter.")
Answer to Popcorn Hack 2:
- take note of use of else statement to ensure you check for even/odd AFTER satisfying operation
- modulo ( % ) operator
try:
num = int(input("Enter a number: ")) # Code that might raise an exception
result = 10 / num # Could raise ZeroDivisionError if num is 0
print("Result:", result)
except ValueError:
print("That's not a valid number!")
except ZeroDivisionError:
print("You can't divide by zero!")
else:
# This block runs if no exception occurs
print("Operation was successful!")
# Check if the number is even or odd
if num % 2 == 0:
print(f"The number {num} is even.")
else:
print(f"The number {num} is odd.")