-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuel.py
More file actions
33 lines (26 loc) · 1.14 KB
/
fuel.py
File metadata and controls
33 lines (26 loc) · 1.14 KB
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
"""
In a file called fuel.py, implement a program that prompts the user for a fraction, formatted as X/Y, wherein each of X and Y is an integer, and then outputs, as a percentage rounded to the nearest integer, how much fuel is in the tank. If, though, 1% or less remains, output E instead to indicate that the tank is essentially empty. And if 99% or more remains, output F instead to indicate that the tank is essentially full.
If, though, X or Y is not an integer, X is greater than Y, or Y is 0, instead prompt the user again. (It is not necessary for Y to be 4.) Be sure to catch any exceptions like ValueError or ZeroDivisionError.
"""
def get_fuel(prompt):
while True:
try:
x, y = str(input(prompt)).split("/")
x, y = int(x), int(y)
fuel = x / y
if fuel > 1:
continue
except (ValueError, ZeroDivisionError):
pass
else:
return fuel
def main():
fuel = get_fuel("Fraction: ")
if fuel <= 0.01:
print("E")
elif fuel >= 0.99:
print("F")
else:
print(f"{fuel:.0%}")
if __name__ == "__main__":
main()