Python L6 Worksheet 2
Python L6 Worksheet 2
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 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 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 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 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")
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()