Skip to the content.

3.3 Hacks

Popcorn and Homework hacks for 3.3

#POPCORN HACK 1

def output(x):
    result = 2 * (x + 3) - 2
    print(result)

output(5)


14
#POPCORN HACK 2

number1 = 8
number2 = 3
number3 = number1 % number2
number4 = number3 * number1 + 70
print(number4)

#Remainder of 8/3 is 2
#2 times 8 is 16
#16 plus 70 is finally 86
86
#POPCORN HACK 3

numbers = [23, 25, 53, 69, 42, 90]


for num in numbers:
    remainder = num % 3
    if remainder == 0:
        print(f"{num} is divisible by 3")
    else:
        print(f"The remainder when {num} is divided by 3 is {remainder}")

The remainder when 23 is divided by 3 is 2
The remainder when 25 is divided by 3 is 1
The remainder when 53 is divided by 3 is 2
69 is divisible by 3
42 is divisible by 3
90 is divisible by 3
#Homework Hack

def fibonacci(n):
    if n < 0:
        raise ValueError("Input must be a non-negative integer.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        a, b = 0, 1
        for _ in range(2, n + 1):
            a, b = b, a + b
        return b

def calculate_area():
    shape = input("Enter the shape (circle, square, rectangle): ").lower()
    
    if shape == "circle":
        radius = float(input("Enter the radius of the circle: "))
        area = 3.14159 * radius ** 2
        print(f"The area of the circle is: {area}")
        
    elif shape == "square":
        side = float(input("Enter the side length of the square: "))
        area = side ** 2
        print(f"The area of the square is: {area}")
        
    elif shape == "rectangle":
        length = float(input("Enter the length of the rectangle: "))
        width = float(input("Enter the width of the rectangle: "))
        area = length * width
        print(f"The area of the rectangle is: {area}")
        
    else:
        print("Invalid shape! Please enter 'circle', 'square', or 'rectangle'.")

n = int(input("Enter the term number for the Fibonacci sequence: "))
print(f"The {n}th term in the Fibonacci sequence is: {fibonacci(n)}")

calculate_area()

Enter the term number for the Fibonacci sequence: 7
The 7th term in the Fibonacci sequence is: 13
Enter the shape (circle, square, rectangle): circle
Enter the radius of the circle: 7
The area of the circle is: 153.93791