Skip to the content.
3.1 and 3.4 3.2 3.3 and 3.5 3.8 3.10

3.8 Hacks

hacks

DevOps

Popcorn Hacks

Popcorn Hack 1


base_ticket_price = 24.99

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

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

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

if has_ticket:
    while True:
        is_student = input("Are you a student? (yes/no): ").lower()
        if is_student == "yes":
            student_discount = 0.20
            final_price = ticket_price * (1 - student_discount)
            print(f"The student discount has brought the price to: ${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.")
Child ticket price: $12.49
The student discount has brought the price to: $10.00

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 num % 2 == 0:
        print(f"The number {num} is even.")
    else:
        print(f"The number {num} is odd.")
Result: 1.0
Operation was successful!
The number 10 is even.

Homework Hack

def get_positive_number():
    while True:
        user_input = input("Please enter a positive number: ")

        try:
            number = float(user_input)
        except ValueError:
            print("Error: That is not a valid number. Please try again.")
            continue

        if number > 0:
            print(f"Success! You entered a positive number: {number}")
            break
        else:
            print("Try again: The number must be positive.")

get_positive_number()
Try again: The number must be positive.
Try again: The number must be positive.
Error: That is not a valid number. Please try again.
Error: That is not a valid number. Please try again.
Error: That is not a valid number. Please try again.
Error: That is not a valid number. Please try again.
Try again: The number must be positive.
Success! You entered a positive number: 2.0