You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

conversions.py 798 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import sys
  2. def convert(value, unit_in, unit_out):
  3. coefficient = get_coefficient(unit_in, unit_out)
  4. return value * coefficient
  5. def get_coefficient(unit_in, unit_out):
  6. distances = {"km": 1 / 1000, "miles": 1 / 1609, "m": 1}
  7. reciprocal_coefficient = 1 / distances[unit_in]
  8. return distances[unit_out] * reciprocal_coefficient
  9. def main():
  10. try:
  11. value = sys.argv[1]
  12. unit_in = sys.argv[2]
  13. unit_out = sys.argv[3]
  14. except IndexError:
  15. print("Pas assez d'arguments")
  16. sys.exit(1)
  17. try:
  18. value = float(value)
  19. except ValueError:
  20. print("Le premier argument doit être un nombre")
  21. sys.exit(1)
  22. result = convert(value, unit_in, unit_out)
  23. print(f"{result:.2f}")
  24. if __name__ == "__main__":
  25. main()