Popcorn Hack #1
Writing a function that will output a y value for y = 4(x+2)-8 when given an input of an x value.
# Defines the function
def output(x):
# Prints the output by first performing x plus 2, then that value multiplied by 4 and finally subtracting 8 from that answer.
print(4 * (x + 2) - 8)
Popcorn Hack #2
Finding the value of number4
number1 = 8
number2 = 3
number3 = number1 % number2 # 8/3 remainder is 2 so number3 = 2
number4 = number3 * number1 + 70 # 2 x 8 + 70
print(number4) # Output will be 86
Popcorn Hack #3
Checking if a list of numbers is divisible by 3.
# List of numbers
numbers = [12, 5, 19, 21]
# For loop that will run through every number in the list of numbers.
for num in numbers:
# Creates a variable which stores the modulus of each number when divided by 3.
remainder = num % 3
# Checks to see if the modulus is 0 meaning there is no remainder when divided.
if remainder == 0:
# Prints that the number is divisible if there is no remainder.
print(num, "is divisible by 3")
# Runs if the remainder is anything other than 0.
else:
# Prints that the remainder when divided by 3 is whatever the modulus was stored as.
print("The remainder when", num, "is divided by 3 is", remainder)
Homework Hack
def fibonacci(n):
if n <= 0:
return "Invalid input. Please enter a positive integer." # needs to be greater than 0
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
import math
def calculate_area():
shape = input("Enter the shape (circle, square, rectangle): ").strip().lower()
if shape == "circle":
radius = float(input("Enter the radius of the circle: "))
area = math.pi * (radius ** 2) # equation for area
print(f"The area of the circle is: {area:.2f}")
elif shape == "square":
side = float(input("Enter the length of a side of the square: "))
area = side ** 2 # equation for side length
print(f"The area of the square is: {area:.2f}")
elif shape == "rectangle":
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width # equation for area
print(f"The area of the rectangle is: {area:.2f}")
else:
print("Invalid shape. Please enter 'circle', 'square', or 'rectangle'.")
def main():
# Get Fibonacci term input
term = int(input("Enter the term number in the Fibonacci sequence: "))
fib_value = fibonacci(term)
print(f"The {term} term in the Fibonacci sequence is: {fib_value}")
# Calculate area of a shape
calculate_area()
# Call the main function
if __name__ == "__main__":
main()
Challenge
# Function to calculate volume of different shapes
def calculate_volume():
shape = input("Enter the shape for volume calculation (rectangular prism, sphere, pyramid): ").strip().lower()
if shape == "rectangular prism":
base = float(input("Enter the base of the prism: "))
width = float(input("Enter the width of the prism: "))
height = float(input("Enter the height of the prism: "))
volume = base * width * height
print(f"The volume of the rectangular prism is: {volume:.2f}")
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:.2f}")
elif shape == "pyramid":
base = float(input("Enter the base 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:.2f}")
else:
print("Invalid shape. Please enter 'rectangular prism', 'sphere', or 'pyramid'.")