Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Basic Unit Converter
- print("Basic Unit Converter:")
- print("")
- #Defining Unit to Convert
- print("METRIC TO IMPERIAL\n \na. Length: KM - Miles\nb. Weight: KG - Pounds\nc. Temperature: Celsius - Fahrenheit\nd. Speed: KM/H -MPH\ne. Volume: Liters - Gallons\n\nIMPERIAL TO METRIC\n \nf. Miles - KM\ng. Pounds - KG\nh. Fahrenheit - Celsius\ni. Km/h - Mph\nj. Liters - Gallons\n")
- def convert_units(choice, value):
- """Converts a value from metric to imperial or vice versa.
- Args:
- choice: The letter corresponding to the unit conversion.
- value: The value to convert.
- """
- #Defining Conversion factors
- #Km to Miles
- if choice == "a":
- converting_unit = "km"
- converted_value = value * 0.621371
- converted_unit = "miles"
- elif choice == "b":
- #Kg to Lbs
- converting_unit = "Kg"
- converted_value = value * 2.20462
- converted_unit = "lbs"
- elif choice == "c":
- #Celsius to Farenheit
- converting_unit = "°C"
- converted_value = (value * 9/5) + 32
- converted_unit = "°F"
- elif choice == "d":
- #Km/H to MP/H
- converting_unit = "km/h"
- converted_value = value * 0.621371
- converted_unit = "MPH"
- elif choice == "e":
- #Liters to Gallons
- converting_unit = "Liters"
- converted_value = value * 0.264172
- converted_unit = "Gal"
- #Miles to Km
- elif choice =="f":
- converting_unit = "miles"
- converted_value = value * 1.60934
- converted_unit = "KM"
- #Lbs to Kg
- elif choice == "g":
- converting_unit = "lbs"
- converted_value = value * 0.453592
- converted_unit = "KG"
- #Farenheit to Celsius
- elif choice == "h":
- converting_unit = "°F"
- converted_value = value * 5/9
- converted_unit = "°C"
- #Km/H to Mph
- elif choice == "i":
- converting_unit = "km/h"
- converted_value = value * 0.621371
- converted_unit = "MPH"
- #Liters to Gallons
- elif choice == "j":
- converting_unit = "Liters"
- converted_value = value * 0.264172
- converted_unit = "Gal"
- else:
- print("Invalid choice. Please enter a letter from 'a' to 'j'. ")
- converted_value = None
- converted_unit = None
- if converted_value is not None:
- print(f"{value} {converting_unit} is equal to {converted_value:.2f} {converted_unit}.")
- return converted_value, converted_unit
- choice = input("Enter the letter corresponding to the unit you want to convert: ").lower()
- value = float(input("Enter the value to convert: "))
- converted_value, converted_unit = convert_units(choice, value)
Advertisement
Add Comment
Please, Sign In to add comment