Advertisement
nikolask

CS50P - PSET3_Fuel.2.py

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