Guest User

Python Project - Unit Converter

a guest
Oct 7th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #Basic Unit Converter
  2. print("Basic Unit Converter:")
  3. print("")
  4.  
  5. #Defining Unit to Convert
  6. 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")
  7.  
  8. def convert_units(choice, value):
  9. """Converts a value from metric to imperial or vice versa.
  10.  
  11. Args:
  12. choice: The letter corresponding to the unit conversion.
  13. value: The value to convert.
  14.  
  15. """
  16. #Defining Conversion factors
  17. #Km to Miles
  18. if choice == "a":
  19. converting_unit = "km"
  20. converted_value = value * 0.621371
  21. converted_unit = "miles"
  22. elif choice == "b":
  23. #Kg to Lbs
  24. converting_unit = "Kg"
  25. converted_value = value * 2.20462
  26. converted_unit = "lbs"
  27. elif choice == "c":
  28. #Celsius to Farenheit
  29. converting_unit = "°C"
  30. converted_value = (value * 9/5) + 32
  31. converted_unit = "°F"
  32. elif choice == "d":
  33. #Km/H to MP/H
  34. converting_unit = "km/h"
  35. converted_value = value * 0.621371
  36. converted_unit = "MPH"
  37. elif choice == "e":
  38. #Liters to Gallons
  39. converting_unit = "Liters"
  40. converted_value = value * 0.264172
  41. converted_unit = "Gal"
  42. #Miles to Km
  43. elif choice =="f":
  44. converting_unit = "miles"
  45. converted_value = value * 1.60934
  46. converted_unit = "KM"
  47. #Lbs to Kg
  48. elif choice == "g":
  49. converting_unit = "lbs"
  50. converted_value = value * 0.453592
  51. converted_unit = "KG"
  52. #Farenheit to Celsius
  53. elif choice == "h":
  54. converting_unit = "°F"
  55. converted_value = value * 5/9
  56. converted_unit = "°C"
  57. #Km/H to Mph
  58. elif choice == "i":
  59. converting_unit = "km/h"
  60. converted_value = value * 0.621371
  61. converted_unit = "MPH"
  62. #Liters to Gallons
  63. elif choice == "j":
  64. converting_unit = "Liters"
  65. converted_value = value * 0.264172
  66. converted_unit = "Gal"
  67. else:
  68. print("Invalid choice. Please enter a letter from 'a' to 'j'. ")
  69. converted_value = None
  70. converted_unit = None
  71. if converted_value is not None:
  72. print(f"{value} {converting_unit} is equal to {converted_value:.2f} {converted_unit}.")
  73. return converted_value, converted_unit
  74.  
  75.  
  76.  
  77. choice = input("Enter the letter corresponding to the unit you want to convert: ").lower()
  78. value = float(input("Enter the value to convert: "))
  79.  
  80. converted_value, converted_unit = convert_units(choice, value)
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment