forked from rohanrdy/CS50-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuel.py
More file actions
21 lines (20 loc) · 636 Bytes
/
fuel.py
File metadata and controls
21 lines (20 loc) · 636 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while True:
f = input("Fraction: ")
f = f.split("/")
try:
f[0] = int(f[0]) # if f[0] cannot be converted to an integer then ValueError will be raised
f[1] = int(f[1])
# if f[1]==0:
# raise ZeroDivisonError
percent = (f[0]/f[1])*100 # we can use line 7 and 8 (and move percent calculation after the while loop) or this. If f[1] is zero then ZeroDivisionError will be raised
except (ValueError, ZeroDivisionError):
pass
else:
if f[0] <= f[1]:
break
if percent >= 99:
print("F")
elif percent <= 1:
print("E")
else:
print(f"{percent:.0f}%")