Advertisement
nikolask

CS50P - PSET5_Fuel.2.py

Aug 20th, 2023
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 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.     while True:
  6.         try:
  7.             text = input("Fraction: ")
  8.             x = int(text.split(sep="/")[0])
  9.             y = int(text.split(sep="/")[1])
  10.             if x > y:
  11.                 raise ValueError
  12.             elif y == 0:
  13.                 raise ZeroDivisionError
  14.             else:
  15.                 outcome = x / y
  16.                 break
  17.         except (ValueError, ZeroDivisionError):
  18.             pass
  19.  
  20.     print(gauge(convert(outcome)))
  21.  
  22. def convert(fraction):
  23.     return round(fraction * 100)
  24.  
  25. def gauge(percentage):
  26.     if percentage <= 1:
  27.         return "E"
  28.     elif percentage >= 99:
  29.         return "F"
  30.     else:
  31.         return f"{percentage}%"
  32.  
  33. if __name__ == "__main__":
  34.     main()
Tags: CS50P
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement