Advertisement
nikolask

CS50P - PSET3_Fuel.1.py

Aug 12th, 2023 (edited)
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | Source Code | 0 0
  1. # https://cs50.harvard.edu/python/2022/psets/3/fuel/
  2. # I'm 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.             return numerator / denominator
  22.         except (ValueError, ZeroDivisionError):
  23.             pass
  24.  
  25.  
  26. main()
Tags: CS50P
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement