# Refueling - problem set 5 - CS50

I have a problem with refueling from problem set 5 of cs50p. I tried and implement a lot of version of the code, always good enough to do the job and pass the test but check50 fails. I give fuel.py and test_fuel.py, I'd be grateful if someone enligth me with the solution: fuel.py:

``````import sys

def main():
percentage = None
fraction = input("Fraction: ")
try:
percentage = convert(fraction)
assert isinstance(percentage, int)

except (ZeroDivisionError, ValueError) as e:
pass
except EOFError:
sys.exit()

if percentage != None:
print(gauge(percentage))

def convert(fraction):
try:
x, y = map(int, fraction.split("/"))
if y != 0:
assert x < y
percentage = round(x/y*100)
return percentage
except AssertionError:
print("Error: X must be less than Y")
raise ValueError
except ValueError:
print("Error: the fraction must be in the format X/Y")
raise ValueError
except ZeroDivisionError:
print("Error: can't divide by 0")
raise ZeroDivisionError

def gauge(percentage):
if percentage <= 1:
return "E"
elif percentage >= 99:
return "F"
else:
return f"{percentage}%"

if __name__ == "__main__":
main()
``````

test_fuel.py:

``````import random, pytest
from fuel import convert, gauge

def main():
test_convert_0()
test_convert_format()
test_convert_float()
test_convert_x_lesser_y()
test_gauge()

def test_convert_0():
fraction = "2/0"
try:
convert(fraction)
print("Error: test did not detect ZeroDivisionError")
except ZeroDivisionError:
print("Test passed: ZeroDivisionError detected")
except Exception as e:
print(f"Error: test did not detect ZeroDivisionError, found anther error {e}")

def test_convert_format():
fraction = "cat/dog"
try:
convert(fraction)
print("Error: test did not detect ValueError")
except ValueError:
print("Test passed: ValueError detected")
except Exception as e:
print(f"Error: test did not detect ZeroDivisionError, found anther error {e}")

def test_convert_float():
fraction = "2/3.5"
try:
convert(fraction)
print("Error: test did not detect ValueError")
except ValueError:
print("Test passed: ValueError detected")
except Exception as e:
print(f"Error: test did not detect ZeroDivisionError, found anther error {e}")

def test_convert_x_lesser_y():
fraction = "2/1"
try:
convert(fraction)
print("Error: test did not detect ValueError")
except ValueError:
print("Test passed: ValueError detected")
except Exception as e:
print(f"Error: test did not detect ZeroDivisionError, found anther error {e}")

def test_gauge():
try:
assert gauge(random.choice([0, 1])) == "E"
except AssertionError:
print("Expected E, but AssertionError was raised")

try:
assert gauge(random.choice([99, 100])) == "F"
except AssertionError:
print("Expected F, but AssertionError was raised")

try:
n = random.choice(list(range(2, 99)))
assert gauge(n) == f"{n}%"
except AssertionError:
print(f"Expected {n}%, but AssertionError was raised")

print("Test gauge function passed")

if __name__ == "__main__":
main()
``````
• what specifically are you failing on in check50? Commented Oct 1, 2023 at 18:34
• Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
– Community Bot
Commented Oct 3, 2023 at 18:07

You don't have any `assert` statements in the 1st four functions. Next, you need to modify your tests for raised exceptions. DON'T use `try:/except:`. Instead use `with pytest.raises(exception-type):`, like this:
``````def test_convert_ystr():
Finally, remove all the extraneous print statements. `test_fuel.py` is intended to be run with `pytest`, not as a python script.
Make those changes and your code should pass `check50`.