-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfuel.py
36 lines (30 loc) · 789 Bytes
/
fuel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def main():
while True:
try:
f = input("Fraction: ")
x = convert(f)
except (ValueError, ZeroDivisionError):
pass
else:
break
p = gauge(x)
print(f"{p}")
def convert(f):
x, y = f.split("/")
if int(y) == 0: # should check for this ZeroDivisionError first since x > y will always be true for y = 0
raise ZeroDivisionError
elif x.isdigit() is False or y.isdigit() is False:
raise ValueError
elif int(x) > int(y):
raise ValueError
else:
return round(int(x) / int(y) * 100)
def gauge(percent):
if percent >= 99:
return "F"
elif percent <= 1:
return "E"
else:
return f"{percent}%"
if __name__ == "__main__":
main()