3.8 Popcorn Hacks
Mihir's Submission for Hacks from Lesson
Popcorn Hack 1
Using this code and nested if statements, check if the user is a student by defining a variable and putting a user input. Modify the code and implement a feature that asks the user if they are a student. If yes is answered, allow a 20 percent discount off of the original 10 dollar ticket. If no is answered, keep the price the same. Make sure it is able to handle invalid responses such as responses other than “yes” or “no” by prompting the user to answer again with a simple “yes” or “no”
CHALLENGE: Add a feature that asks for the age of the user, and make varying ticket prices based off of the age groups.
Ages 0-12: Child Ticket (50% off) Ages 13-63 Adult Ticket (0% off) Ages 65+: Senior Ticket (30% off) Popcorn Hack
def get_student_discount():
while True:
student = input("Are you a student? (yes/no): ").strip().lower()
if student == "yes":
return 0.8 # 20% discount
elif student == "no":
return 1 # no discount
else:
print("Invalid response. Please answer with 'yes' or 'no'.")
def get_age_discount():
while True:
try:
age = int(input("Please enter your age: "))
if age < 0:
print("Invalid age. Try again.")
continue
if age <= 12:
return 0.5 # 50% off for children
elif 13 <= age <= 63:
return 1 # no discount for adults
else:
return 0.7 # 30% off for seniors
except ValueError:
print("Please enter a valid age.")
def calculate_price():
base_price = 10
student_discount = get_student_discount()
age_discount = get_age_discount()
final_price = base_price * student_discount * age_discount
print(f"Your final ticket price is: ${final_price:.2f}")
calculate_price()
Your final ticket price is: $8.00
Popcorn Hack 2
Using this code, check if the number is even or odd, after satisfying the successful operation.
def check_even_odd():
while True:
try:
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
break
except ValueError:
print("Please enter a valid integer.")
check_even_odd()
17 is odd.