Homework Hack

Review the instructions and examples in the lesson and create a function that will produce a certain term in the fibonacci sequence that is inputted as well as one that can find the area of a circle, square, and a rectangle by asking for input on what the shape is, and what the value(s) are that are needed to calculate the area of the shape.

Challenge: Add one that can do the volume of rectangluar prisms, spheres, and pyramids.

Formulas:

  • Volume of rectangluar prism is base x width x height
  • Volume of sphere is 4/3 x pi x r cubed
  • Volume of pyramid is 1/3 x base x width x height

Homework Hack 1: Fibonacci

def fibonacci(n):
    if n <= 0:
        return "Input should be a positive integer"
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a, b = 0, 1
        for _ in range(2, n):
            a, b = b, a + b
        return b

# Example usage:
term = 10
print(f"The {term}th Fibonacci number is: {fibonacci(term)}")

The 10th Fibonacci number is: 34

Homework Hack 2: Area Function for Shapes

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

# Example usage:
calculate_area()
The area of the circle is: 78.53975

Homework Hack 3: Volume Calculation for 3D Shapes

def calculate_volume():
    shape = input("Enter the 3D shape (prism, sphere, pyramid): ").lower()
    if shape == "prism":
        base = float(input("Enter the base: "))
        width = float(input("Enter the width: "))
        height = float(input("Enter the height: "))
        volume = base * width * height
        print(f"The volume of the rectangular prism is: {volume}")
    elif shape == "sphere":
        radius = float(input("Enter the radius: "))
        volume = (4/3) * 3.14159 * radius ** 3
        print(f"The volume of the sphere is: {volume}")
    elif shape == "pyramid":
        base = float(input("Enter the base: "))
        width = float(input("Enter the width: "))
        height = float(input("Enter the height: "))
        volume = (1/3) * base * width * height
        print(f"The volume of the pyramid is: {volume}")
    else:
        print("Unknown shape!")

# Example usage:
calculate_volume()
The volume of the pyramid is: 105.0