Skip to the content.

3.8 Hacks

Tri 1 Team Teach Hacks

Popcorn Hack 1

age = int(input("What is your age?"))
has_ticket = True
try:
    student = input("Are you a student?")
    if student not in ["Yes","yes","No","no"]:
        raise ValueError("This is a yes or no question.")
except ValueError as e:
    print(e)
if age >= 18:
    print("You are old enough to watch the movie.")
    if has_ticket:
        print("You can enter the theater.")
    else:
        print("You need a ticket to enter.")
        if student == "Yes":
            print("You get a 20% discount of the $10 ticket!")
        else:
            print("You don't get a discount on your $10 ticket.")
else:
    print("You are too young to watch this movie.")
if 0 <= age <= 12:
    print("You get 50% off your $10 ticket!")
    if student == "Yes":
        print("You get a 20% more discount on your ticket!")
    else:
        print("You don't get another discount :(.")
elif 13 <= age <= 65:
    print("You don't get a discount on your ticket.")
    if student == "Yes":
        print("You get a 20% discount off the $10 ticket!")
    else:
        print("You don't get a discount on your $10 ticket.")
else:
    print("You get 30% of your $10 ticket!")
    if student == "Yes":
        print("You get a 20% more discount on your ticket!")
    else:
        print("You don't get another discount on your ticket :(.")

Popcorn Hack 2

try:
    num = int(input("Enter a number: "))  
    result = 10 / num  
    print("Result:", result)
except ValueError:
    print("That's not a valid number!")
except ZeroDivisionError:
    print("You can't divide by zero!")
else:
    print("Operation was successful!")
if result % 2 == 0:
    print("The result is even!")
elif result % 2 == 1:
    print("The result is odd!")
else:
    print("The result is neither odd nor even!")

Homework Hack

def get_positive_integer(prompt):
    while True:
        user_input = input(prompt)
        try:
            value = int(user_input)
            if value <= 0:
                print("The number must be positive. Please try again.")
            else:
                print("Success!")
                return value
        except ValueError:
            print("Invalid input. Please enter a valid positive integer.")
num = get_positive_integer("What is your favorite number?")
print("Your favorite number is: ", num)