My code for fuel.py:
def main():
while True:
# Prompting the user to enter a fraction
fraction = input("Fraction: ")
# Calling check_fraction() function to validate the input
x, y = convert(fraction)
# If x or y is None, prompting the user again to enter a valid fraction
if x is None or y is None:
continue
# Calculating the percentage of the fraction
percentage = int(round(x / y * 100))
# Output E if the percentage is less than or equal to 1
if percentage <= 1:
print(gauge(percentage))
# Output F if the percentage is greater than or equal to 99
elif percentage >= 99:
print(gauge(percentage))
# Output the percentage as a percentage value
else:
print(f"{gauge(percentage)}%")
break
def convert(fraction):
try:
# Splitting the fraction by '/' and convert the values to integers
x, y = map(int, fraction.split("/"))
# Raising ValueError if x is greater than y or y is less than or equal to zero
if x > y or not str(x).isnumeric() or not str(y).isnumeric() or y==0:
# Returning None for x and y if an exception is raised
return None, None
#frac=x/y I included this line to create a situation for ZeroDivisionError, but it did not work
# Returning x and y if the fraction is valid
return x, y
# Handle ValueError and ZeroDivisionError exceptions
except (ValueError, ZeroDivisionError):
raise
def gauge(percentage):
if percentage <= 1:
return "E"
elif percentage >= 99:
return "F"
else:
return percentage
if __name__ == "__main__":
main()
My code for checking ZeroDivisionError in test_fuel.py:
def test_zero_division():
with pytest.raises(ZeroDivisionError):
fuel.convert("5/0")