Skip to the content.

3.8 Hacks

hacks

Popcorn Hack 1

base_ticket_price = 10.00

age = int(input("Please enter your age: "))

if age <= 12:
    ticket_price = base_ticket_price * 0.5  
    print(f"Child ticket price: ${ticket_price:.2f}")
elif age <= 63:
    ticket_price = base_ticket_price 
    print(f"Adult ticket price: ${ticket_price:.2f}")
else:
    ticket_price = base_ticket_price * 0.7
    print(f"Senior ticket price: ${ticket_price:.2f}")
Child ticket price: $5.00

Challenge


has_ticket = input("Do you have a ticket? (yes/no): ").lower() == "yes"

ticket_price = 10.00

if has_ticket:
   
    age = int(input("Please enter your age: "))
    
    if age <= 12:
        final_price = ticket_price * 0.50  
        print(f"Child ticket applied! The ticket price is: ${final_price:.2f}")
    elif age >= 65:
        final_price = ticket_price * 0.70  
        print(f"Senior ticket applied! The ticket price is: ${final_price:.2f}")
    else:
        final_price = ticket_price  
        print(f"Adult ticket, no discount applied. The ticket price is: ${final_price:.2f}")
else:
    print("You need a ticket to enter.")

Senior ticket applied! The ticket price is: $7.00

Popcorn Hack 2

try:
    number = int(input("Enter a number: ")) 
    result = 10 / number 
    print("Result:", result)
except ValueError:
    print("Not a number")
except ZeroDivisionError:
    print("Can't divide by 0")
else:
   
    print("successful")
    
    if number % 2 == 0:
        print(f"The number {number} is even.")
    else:
        print(f"The number {number} is odd.")
Result: 1.6666666666666667
successful
The number 6 is even.

HW Hack

while True:
    try:
        number = int(input("Enter a positive number: "))
        
        if number <= 0:
            print("Try again! Please enter a positive number.")
            continue
        
        if number % 2 != 0:
            print("This is a positive number.")
            continue

        result = 10 / number
        print(f"Result: {result:.2f}")

    except ValueError:
        print("Error: Not a valid number.")
    except ZeroDivisionError:
        print("Error: Cannot divide by zero.")
    else:
        print("Success!") 
        break  


This is a positive number.
Result: 5.00
Success!