0% found this document useful (0 votes)
7 views9 pages

Python L6 Worksheet 2

The document contains a series of Python functions for converting various units of measurement, calculating geometric shapes' properties, and a menu-driven interface for user interaction. It includes functions for converting inches to centimeters, miles to kilometers, and calculating the volume and surface area of cuboids, triangular prisms, and cylinders. The main function orchestrates user input and displays results based on the selected conversion or calculation.

Uploaded by

mzama21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Python L6 Worksheet 2

The document contains a series of Python functions for converting various units of measurement, calculating geometric shapes' properties, and a menu-driven interface for user interaction. It includes functions for converting inches to centimeters, miles to kilometers, and calculating the volume and surface area of cuboids, triangular prisms, and cylinders. The main function orchestrates user input and displays results based on the selected conversion or calculation.

Uploaded by

mzama21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

def inch_to_cm(inches):

"""Converts inches to centimeters."""


return inches * 2.54

def mile_to_km(miles):
"""Converts miles to kilometers."""
return miles * 1.6093

def foot_to_m(feet):
"""Converts feet to meters."""
return feet * 0.3048

def yard_to_m(yards):
"""Converts yards to meters."""
return yards * 0.9144

def gallon_to_l(gallons):
"""Converts gallons to liters."""
return gallons * 4.546

def pound_to_kg(pounds):
"""Converts pounds to kilograms."""
return pounds * 0.454

def ounce_to_g(ounces):
"""Converts ounces to grams."""
return ounces * 28.35

def display_menu():
"""Displays the conversion menu."""
print("Conversion Menu:")
print("1. Inches to Centimeters")
print("2. Miles to Kilometers")
print("3. Feet to Meters")
print("4. Yards to Meters")
print("5. Gallons to Liters")
print("6. Pounds to Kilograms")
print("7. Ounces to Grams")
print("8. Exit")

def get_user_choice():
"""Gets the user's choice from the menu."""
while True:
try:
choice = int(input("Enter your choice: "))
if 1 <= choice <= 8:
return choice
else:
print("Invalid choice. Please enter a number between 1 and 8.")
except ValueError:
print("Invalid input. Please enter a number.")

def main():
"""Main function for the conversion program."""
while True:
display_menu()
choice = get_user_choice()

if choice == 1:
inches = float(input("Enter inches: "))
cm = inch_to_cm(inches)
print(f"{inches} inches is equal to {cm:.2f} centimeters.")
elif choice == 2:
miles = float(input("Enter miles: "))
km = mile_to_km(miles)
print(f"{miles} miles is equal to {km:.2f} kilometers.")
elif choice == 3:
feet = float(input("Enter feet: "))
m = foot_to_m(feet)
print(f"{feet} feet is equal to {m:.2f} meters.")
elif choice == 4:
yards = float(input("Enter yards: "))
m = yard_to_m(yards)
print(f"{yards} yards is equal to {m:.2f} meters.")
elif choice == 5:
gallons = float(input("Enter gallons: "))
l = gallon_to_l(gallons)
print(f"{gallons} gallons is equal to {l:.2f} liters.")
elif choice == 6:
pounds = float(input("Enter pounds: "))
kg = pound_to_kg(pounds)
print(f"{pounds} pounds is equal to {kg:.2f} kilograms.")
elif choice == 7:
ounces = float(input("Enter ounces: "))
g = ounce_to_g(ounces)
print(f"{ounces} ounces is equal to {g:.2f} grams.")
elif choice == 8:
print("Exiting the program.")
break

if __name__ == "__main__":
main()
def inch_to_cm(inches):
"""Converts inches to centimeters."""
return inches * 2.54, "cm"

def mile_to_km(miles):
"""Converts miles to kilometers."""
return miles * 1.6093, "km"

def foot_to_m(feet):
"""Converts feet to meters."""
return feet * 0.3048, "m"

def yard_to_m(yards):
"""Converts yards to meters."""
return yards * 0.9144, "m"

def gallon_to_l(gallons):
"""Converts gallons to liters."""
return gallons * 4.546, "l"

def pound_to_kg(pounds):
"""Converts pounds to kilograms."""
return pounds * 0.454, "kg"
def ounce_to_g(ounces):
"""Converts ounces to grams."""
return ounces * 28.35, "g"

def display_menu():
"""Displays the conversion menu."""
print("Conversion Menu:")
print("1. Inches to Centimeters")
print("2. Miles to Kilometers")
print("3. Feet to Meters")
print("4. Yards to Meters")
print("5. Gallons to Liters")
print("6. Pounds to Kilograms")
print("7. Ounces to Grams")
print("8. Exit")

def get_user_choice():
"""Gets the user's choice from the menu."""
while True:
try:
choice = int(input("Enter your choice: "))
if 1 <= choice <= 8:
return choice
else:
print("Invalid choice. Please enter a number between 1 and 8.")
except ValueError:
print("Invalid input. Please enter a number.")

def main():
"""Main function for the conversion program."""
while True:
display_menu()
choice = get_user_choice()

if choice == 1:
inches = float(input("Enter inches: "))
cm, unit = inch_to_cm(inches)
print(f"{inches} inches is equal to {cm:.2f} {unit}.")
elif choice == 2:
miles = float(input("Enter miles: "))
km, unit = mile_to_km(miles)
print(f"{miles} miles is equal to {km:.2f} {unit}.")
elif choice == 3:
feet = float(input("Enter feet: "))
m, unit = foot_to_m(feet)
print(f"{feet} feet is equal to {m:.2f} {unit}.")
elif choice == 4:
yards = float(input("Enter yards: "))
m, unit = yard_to_m(yards)
print(f"{yards} yards is equal to {m:.2f} {unit}.")
elif choice == 5:
gallons = float(input("Enter gallons: "))
l, unit = gallon_to_l(gallons)
print(f"{gallons} gallons is equal to {l:.2f} {unit}.")
elif choice == 6:
pounds = float(input("Enter pounds: "))
kg, unit = pound_to_kg(pounds)
print(f"{pounds} pounds is equal to {kg:.2f} {unit}.")
elif choice == 7:
ounces = float(input("Enter ounces: "))
g, unit = ounce_to_g(ounces)
print(f"{ounces} ounces is equal to {g:.2f} {unit}.")
elif choice == 8:
print("Exiting the program.")
break

if __name__ == "__main__":
main()
import math

def getRadius():
"""Gets the radius of the circle from the user."""
radius = float(input("Enter the radius of the circle: "))
return radius

def calculateCircumference(radius):
"""Calculates the circumference of the circle."""
circumference = 2 * math.pi * radius
return circumference

def calculateArea(radius):
"""Calculates the area of the circle."""
area = math.pi * radius * radius
return area

def displayResults(circumference, area):


"""Displays the calculated circumference and area."""
print(f"The circumference of the circle is: {circumference:.2f}")
print(f"The area of the circle is: {area:.2f}")

def main():
"""Main function for the circle calculation program."""
radius = getRadius()
circumference = calculateCircumference(radius)
area = calculateArea(radius)
displayResults(circumference, area)

if __name__ == "__main__":
main()
import math

def get_cuboid_dimensions():
"""Gets the length, width, and height of the cuboid from the user."""
length = float(input("Enter the length of the cuboid: "))
width = float(input("Enter the width of the cuboid: "))
height = float(input("Enter the height of the cuboid: "))
return length, width, height

def calculate_cuboid_volume(length, width, height):


"""Calculates the volume of the cuboid."""
volume = length * width * height
return volume

def calculate_cuboid_surface_area(length, width, height):


"""Calculates the total surface area of the cuboid."""
surface_area = 2 * (length * width + length * height + width * height)
return surface_area

def display_cuboid_results(volume, surface_area):


"""Displays the calculated volume and surface area of the cuboid."""
print(f"The volume of the cuboid is: {volume:.2f}")
print(f"The total surface area of the cuboid is: {surface_area:.2f}")

def get_triangular_prism_dimensions():
"""Gets the base length, base height, and height of the triangular prism from the
user."""
base_length = float(input("Enter the base length of the triangular prism: "))
base_height = float(input("Enter the base height of the triangular prism: "))
height = float(input("Enter the height of the triangular prism: "))
return base_length, base_height, height

def calculate_triangular_prism_volume(base_length, base_height, height):


"""Calculates the volume of the triangular prism."""
volume = 0.5 * base_length * base_height * height
return volume

def calculate_triangular_prism_surface_area(base_length, base_height, height):


"""Calculates the total surface area of the triangular prism."""
base_area = 0.5 * base_length * base_height
lateral_area = (base_length + base_height + math.sqrt(base_length**2 +
base_height**2)) * height
surface_area = 2 * base_area + lateral_area
return surface_area

def display_triangular_prism_results(volume, surface_area):


"""Displays the calculated volume and surface area of the triangular prism."""
print(f"The volume of the triangular prism is: {volume:.2f}")
print(f"The total surface area of the triangular prism is: {surface_area:.2f}")

def get_cylinder_dimensions():
"""Gets the radius and height of the cylinder from the user."""
radius = float(input("Enter the radius of the cylinder: "))
height = float(input("Enter the height of the cylinder: "))
return radius, height

def calculate_cylinder_volume(radius, height):


"""Calculates the volume of the cylinder."""
volume = math.pi * radius**2 * height
return volume

def calculate_cylinder_surface_area(radius, height):


"""Calculates the total surface area of the cylinder."""
surface_area = 2 * math.pi * radius * (radius + height)
return surface_area

def display_cylinder_results(volume, surface_area):


"""Displays the calculated volume and surface area of the cylinder."""
print(f"The volume of the cylinder is: {volume:.2f}")
print(f"The total surface area of the cylinder is: {surface_area:.2f}")

def main():
"""Main function for the geometric shape calculation program."""
while True:
print("Choose a shape:")
print("1. Cuboid")
print("2. Triangular Prism")
print("3. Cylinder")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
length, width, height = get_cuboid_dimensions()
volume = calculate_cuboid_volume(length, width, height)
surface_area = calculate_cuboid_surface_area(length, width, height)
display_cuboid_results(volume, surface_area)
elif choice == '2':
base_length, base_height, height = get_triangular_prism_dimensions()
volume = calculate_triangular_prism_volume(base_length, base_height, height)
surface_area = calculate_triangular_prism_surface_area(base_length,
base_height, height)
display_triangular_prism_results(volume, surface_area)
elif choice == '3':
radius, height = get_cylinder_dimensions()
volume = calculate_cylinder_volume(radius, height)
surface_area = calculate_cylinder_surface_area(radius, height)
display_cylinder_results(volume, surface_area)
elif choice == '4':
print("Exiting the program.")
break
else:
print("Invalid choice. Please enter a number between 1 and 4.")

if __name__ == "__main__":
main()

You might also like