浏览代码

Add conversions.py

master
Dimitri Merejkowsky 5 年前
父节点
当前提交
8095fdd480
共有 1 个文件被更改,包括 38 次插入0 次删除
  1. +38
    -0
      sources/conversions/conversions.py

+ 38
- 0
sources/conversions/conversions.py 查看文件

@@ -0,0 +1,38 @@
import sys


def convert(value, unit_in, unit_out):
coefficient = get_coefficient(unit_in, unit_out)
return value * coefficient


def get_coefficient(unit_in, unit_out):
distances = {"km": 1 / 1000, "miles": 1 / 1609, "m": 1}

reciprocal_coefficient = 1 / distances[unit_in]

return distances[unit_out] * reciprocal_coefficient


def main():
try:
value = sys.argv[1]
unit_in = sys.argv[2]
unit_out = sys.argv[3]

except IndexError:
print("Pas assez d'arguments")
sys.exit(1)

try:
value = float(value)
except ValueError:
print("Le premier argument doit être un nombre")
sys.exit(1)

result = convert(value, unit_in, unit_out)
print(f"{result:.2f}")


if __name__ == "__main__":
main()