If I "return None" explicitly for exceptions in a regular .py file, when I do the test.py file, is it correct to test with a "assert function(argument) is None?" I did a pytest and it worked fine, but when I did a CS50 check, it gave me:
":( correct fuel.py passes all test_fuel checks
expected exit code 0, not 1"
If I remove the test cases under test_convert_invalid(), CS50 did not give me the exist code 0 prompt, but it gave errors on checks for "ValueError" and "ZeroDivisionError".
Is my assert wrong under def test_convert_invalid()? If it is, why does the test file passed with pytest? How can I fix the issue?
I returned "None" explicitly because return False will be equivalent to 0, which will result in "E" (Empty tank).
`fuel.py`
import sys
def main():
while True:
try:
# convert() expects a str and returns an int
percentage = convert(input("Fraction: "))
# gauge() expects an int and returns a str
print(gauge(percentage))
except:
continue
else:
sys.exit()
def convert(fraction):
try:
x, y = fraction.split("/")
if not x[:].isdigit() or not y[:].isdigit():
raise TypeError
if y == "0":
raise ZeroDivisionError
if int(x) > int(y) and int(y) != 0:
raise ValueError
except TypeError:
return None
except ZeroDivisionError:
return None
except ValueError:
return None
else:
return int((float(x) / float(y)) * 100)
def gauge(percentage):
if 0 <= percentage <= 1:
return "E"
elif percentage >= 99:
return "F"
else:
return str(percentage) + "%"
if __name__ == "__main__":
main()
`test_fuel.py`
from fuel import convert, gauge
def test_convert_valid():
assert convert("3/4") == 75
assert convert("100/100") == 100
assert convert("99/100") == 99
assert convert("0/100") == 0
assert convert("1/100") == 1
def test_convert_invalid():
assert convert("1/0") is None
assert convert("4/3") is None
assert convert("test") is None
assert convert("t/1") is None
assert convert("1/t") is None
def test_gauge():
assert gauge(0) == "E"
assert gauge(1) == "E"
assert gauge(100) == "F"
assert gauge(99) == "F"
assert gauge(75) == "75%"