nikolask

CS50P - PSET5_Fuel_FINAL.py

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