A simple measurement conversion utility.
★★★The unica was one-twelfth of a Roman foot. In 1324 the legal definition of the inch was set out in a statute of Edward II of England, defining it as "three grains of barley, dry and round, placed end to end, lengthwise". At various times the inch has also been defined as the combined lengths of 12 poppyseeds. Since 1959 the inch has been defined officially as 2.54cm.
Write a program that converts feet to inches and inches to feet. The user should be prompted to enter the conversion they require.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
feet_to_inches that:feet and returns inches calculated as feet multipled by twelve.inches_to_feet that:inches and returns feet calculated as inches divided by twelve.menu that:converter that:converter subprogram.1. Feet to inches
2. Inches to feet
3. Quit
Enter choice: 1
Enter the number of feet: 3
3.0 feet is 36.0 inches.
1. Feet to inches
2. Inches to feet
3. Quit
Enter choice: 2
Enter the number of inches: 72
72.0 inches is 6.0 feet.
1. Feet to inches
2. Inches to feet
3. Quit
Enter choice: 3
Goodbye
# Measurements program
# -------------------------
# Subprograms
# -------------------------
# Convert feet to inches
def feet_to_inches(feet):
return feet * 12
# Convert inches to feet
def inches_to_feet(inches):
return inches / 12
# Offer the conversion choice to the user
def menu():
menu_choice = "0"
# Iterate until a valid option is chosen
while menu_choice < "1" or menu_choice > "3":
print("1. Feet to inches")
print("2. Inches to feet")
print("3. Quit")
menu_choice = input("Enter choice: ")
return menu_choice
# Call conversion routines from user's choice
def converter():
option = menu()
match option:
# Feet to inches
case "1":
feet = float(input("Enter the number of feet: "))
inches = feet_to_inches(feet)
print(feet, "feet is", inches, "inches.")
# Inches to feet
case "2":
inches = float(input("Enter the number of inches: "))
feet = inches_to_feet(inches)
print(inches, "inches is", feet, "feet.")
# Quit
case "3":
print("Goodbye")
# -------------------------
# Main program
# -------------------------
converter()