Skip to the content.

3.8 Hacks

Python Hacks

Popcorn Hack #1

initial_price = 10

age = int(input("Enter your age:"))
if age <= 12:
    i_price = initial_price * 0.5 
    print(f"Child ticket price: ${i_price:.2f}")
elif age <= 63:
    i_price = initial_price
    print(f"Adult ticket price: ${i_price:.2f}")
else:
    i_price = initial_price * 0.7
    print(f"Senior ticket price: ${i_price:.2f}")

is_student = input("Are you a student? (yes/no)")
if is_student == 'yes':
    fprice = i_price * 0.8
    print(f"The final price is ${fprice}")
elif is_student == 'no':
    fprice = i_price
    print(f"The final price is ${fprice}")
else:
    print('please answer yes/no')

Popcorn Hack #2

try:
    num = int(input("Enter a number: ")) 
    result = 10 / num 
except ValueError:
    print("Please enter a valid number, make sure it's an integer")
except ZeroDivisionError:
    print("It can't be 0!")
else:    
    if num % 2 == 0:
        print(f"The number {num} is even.")
    else:
        print(f"The number {num} is odd.")

Homework Hack

r = True
while r == True:
    try:
        num = float(input("Enter a number: ")) 
        result = 10 / num 
    except ValueError:
        print("Error. Please enter a valid number")
    except ZeroDivisionError:
        print("It can't be 0!")
    else:    
        if num > 0:
            print(f"Success! {num} is positive.")
            r = False
        else:
            print(f"The number {num} is negative, try again.")