Queer European MD passionate about IT

fuel.py 697 B

1234567891011121314151617181920212223242526272829303132333435
  1. def main():
  2. while True:
  3. fuel = input("Fraction:\t\t")
  4. try:
  5. result = convert(fuel)
  6. break
  7. except (ValueError, ZeroDivisionError):
  8. continue
  9. print(gauge(result))
  10. def convert(fraction: str) -> int:
  11. try:
  12. x, y = map(int, fraction.split('/'))
  13. except (ValueError, TypeError):
  14. raise ValueError
  15. if x > y:
  16. raise ValueError
  17. if y == 0:
  18. raise ZeroDivisionError
  19. return int(round(x / y*100, 0))
  20. def gauge(percentage):
  21. if percentage <= 1:
  22. return "E"
  23. elif percentage >= 99:
  24. return "F"
  25. else:
  26. return f"{percentage}%"
  27. if __name__ == "__main__":
  28. main()