Advertisement
nikolask

CS50P - PSET5_Fuel.1.py

Aug 20th, 2023 (edited)
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | Source Code | 0 0
  1. # https://cs50.harvard.edu/python/2022/psets/3/fuel/
  2. # rewritten for test_fuel.py
  3.  
  4. def main():
  5.     text = input("Fraction: ")
  6.     print(gauge(convert(text)))
  7.  
  8. def convert(fraction):
  9.     while True:
  10.         try:
  11.             x = int(fraction.split(sep="/")[0])
  12.             y = int(fraction.split(sep="/")[1])
  13.             if x > y:
  14.                 raise ValueError
  15.             elif y == 0:
  16.                 raise ZeroDivisionError
  17.             else:
  18.                 return round(x / y * 100)
  19.         except (ValueError, ZeroDivisionError):
  20.             fraction = input("Fraction: ")
  21.             pass
  22.  
  23. def gauge(percentage):
  24.     if percentage <= 1:
  25.         return "E"
  26.     elif percentage >= 99:
  27.         return "F"
  28.     else:
  29.         return f"{percentage}%"
  30.  
  31. if __name__ == "__main__":
  32.     main()
Tags: CS50P
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement