Popcorn Hacks
#1
def math(x):
print(7 * (x + 4) - 6)
#2
numbers = [4, 9, 15, 23, 30, 44, 51, 63, 77, 89]
for number in numbers:
if number % 3 == 0:
print(f"{number} is divisible by 3")
else:
remainder = number % 3
print(f"{number} is not divisible by 3, remainder is {remainder}")
Homework Hack
import math
def fibonacci(n):
if n <= 0:
return "Invalid input. n must be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
else:
a, b = 0, 1
for i in range(2, n):
a, b = b, a + b
return b
def calculate_area():
shape = input("What shape do you want to calculate the area for? (circle, square, rectangle): ").lower()
if shape == "circle":
radius = float(input("Enter the radius of the circle: "))
area = math.pi * 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 entered.")
def calculate_volume():
shape = input("What shape do you want to calculate the volume for? (rectangular prism, sphere, pyramid): ").lower()
if shape == "rectangular prism":
length = float(input("Enter the length of the rectangular prism: "))
width = float(input("Enter the width of the rectangular prism: "))
height = float(input("Enter the height of the rectangular prism: "))
volume = length * width * height
print(f"The volume of the rectangular prism is: {volume}")
elif shape == "sphere":
radius = float(input("Enter the radius of the sphere: "))
volume = (4/3) * math.pi * radius ** 3
print(f"The volume of the sphere is: {volume}")
elif shape == "pyramid":
base = float(input("Enter the base length of the pyramid: "))
width = float(input("Enter the width of the pyramid: "))
height = float(input("Enter the height of the pyramid: "))
volume = (1/3) * base * width * height
print(f"The volume of the pyramid is: {volume}")
else:
print("Invalid shape entered.")
n = int(input("Enter the term number of the Fibonacci sequence you want: "))
print(f"The {n}th term in the Fibonacci sequence is: {fibonacci(n)}")
calculate_area()
calculate_volume()