Skip to the content.

3.7 Hacks!

3.7 Popcorn and Homework Hacks

Popcorn Hack #1

if else statments in python

score = 85

if score >= 90:
    print("A")
else:
    if score >= 80:
        print("B")
    else:
        if score >= 70:
            print("C")
        else:
            if score >= 60:
                print("D")
            else:
                print("F")

Popcorn Hack #2

Iis eligible for a loan based on their credit score and income:

credit_score = 750
income = 60000

if credit_score >= 700:
    if income >= 50000:
        print("Eligible for loan")
    else:
        print("Not eligible for loan due to low income")
else:
    print("Not eligible for loan due to low credit score")

Popcorn Hack #3 in pseudocode

IF is_member = TRUE { IF purchase_amount > 100 { DISPLAY “20% discount” } ELSE { DISPLAY “10% discount” } } ELSE { IF purchase_amount > 100 { DISPLAY “5% discount” } ELSE { DISPLAY “No discount” } }

Homework Hack

Write pseudocode to determine if a student passes a class based on their exam scores and attendance using nested conditionals.

BEGIN INPUT examScore INPUT attendancePercentage

IF examScore >= 60 THEN
    IF attendancePercentage >= 75 THEN
        PRINT "Student passes the class."
    ELSE
        PRINT "Student fails due to low attendance."
    ENDIF
ELSE
    PRINT "Student fails due to low exam score."
ENDIF END

Write a python segment to decide the shipping cost based on the weight of a package and the delivery speed chosen (standard or express) using nested conditionals.

# Get input from the user
weight = float(input("Enter the weight of the package (in pounds): "))
delivery_speed = input("Choose delivery speed (standard or express): ").strip().lower()

# Determine shipping cost
if weight <= 5:
    if delivery_speed == "standard":
        cost = 5.00
    elif delivery_speed == "express":
        cost = 10.00
    else:
        print("Invalid delivery speed.")
        cost = None
elif weight <= 20: 
    if delivery_speed == "standard":
        cost = 10.00
    elif delivery_speed == "express":
        cost = 20.00
    else:
        print("Invalid delivery speed.")
        cost = None
else:
    if delivery_speed == "standard":
        cost = 20.00
    elif delivery_speed == "express":
        cost = 40.00
    else:
        print("Invalid delivery speed.")
        cost = None

if cost is not None:
    print(f"The shipping cost is: ${cost:.2f}")

Write a python segment to have different ticket prices for different ages, with a discount for students

# Get input from the user
age = int(input("Enter your age: "))
is_student = input("Are you a student? (yes or no): ").strip().lower()

# Determine ticket price
if age < 13:
    ticket_price = 8.00  # Child price
elif 13 <= age < 65:
    ticket_price = 12.00  # Adult price
else:
    ticket_price = 10.00  # Senior price

# Apply student discount
if is_student == "yes":
    ticket_price *= 0.90  # 10% discount

print(f"The ticket price is: ${ticket_price:.2f}")

Challenge Hack

# Function to check if the sides can form a triangle
def is_valid_triangle(a, b, c):
    return (a + b > c) and (a + c > b) and (b + c > a)

# Get input from the user with validation
while True:
    try:
        side1 = float(input("Enter the length of the first side (positive number): "))
        side2 = float(input("Enter the length of the second side (positive number): "))
        side3 = float(input("Enter the length of the third side (positive number): "))
        
        if side1 <= 0 or side2 <= 0 or side3 <= 0:
            print("Please enter positive numbers only.")
            continue
        
        break  # Exit loop if inputs are valid
    except ValueError:
        print("Invalid input. Please enter numeric values.")

# Check if the sides can form a triangle
if is_valid_triangle(side1, side2, side3):
    # Classify the type of triangle
    if side1 == side2 == side3:
        print("The triangle is an Equilateral Triangle.")
    elif side1 == side2 or side1 == side3 or side2 == side3:
        print("The triangle is an Isosceles Triangle.")
    else:
        print("The triangle is a Scalene Triangle.")
else:
    print("The lengths entered do not form a valid triangle.")