enter image description hereI fail to spot what is wrong on this code yet pytest runs succesfully but I get error messages when CS50 test my code.
I expect my code to raise ValuError, TypeError.Exceptions and be testable meet all requirements such as outputing the right percentage.
Here is My code of fuel.py
def main():
while True:
try:
fuel = input("Fraction:").split('/')
fraction = convert(fuel)
if fraction is not None:
print(fraction)
percentage = gauge(fuel)
if percentage is not None:
print(percentage)
break
except ValueError:
raise ValueError("ValueError")
except ZeroDivisionError:
raise ZeroDivisionError("Error")
except TypeError:
raise TypeError("ValueError")
def convert(fraction):
fraction = fraction
x = int(fraction[0])
y = int(fraction[1])
if y == 0:
raise ZeroDivisionError("ZeroDivisionError")
fraction = round((x/y)*100)
if fraction > 100:
raise ValueError("ValueError")
else:
if fraction < 99 and fraction > 1:
return fraction
def gauge(percentage):
fraction = percentage
x = int(fraction[0])
y = int(fraction[1])
percentage = round((x/y)*100)
if fraction[0] == fraction[1] or percentage >= 99 :
return "F"
if percentage <= 1:
return "E"
else:
return str(percentage) + "%"
if __name__=="__main__":
main()
My Test file is this one. below
import pytest
from fuel import convert, gauge
def test_convert():
assert convert("3", "4") == "75%"
def test_convert():
with pytest.raises(ZeroDivisionError):
convert(["3", "0"])
def test_convert():
with pytest.raises(ValueError):
convert(["4", "3"])
def test_gauge():
assert gauge("7", "7") == "F"
def test_gauge():
assert gauge("1", "100") == "E"
However the instructions were In a file called fuel.py, reimplement Fuel Gauge from Problem Set 3, restructuring your code per the below, wherein:
convert expects a str in X/Y format as input, wherein each of X and Y is an integer, and returns that fraction as a percentage rounded to the nearest int between 0 and 100, inclusive. If X and/or Y is not an integer, or if X is greater than Y, then convert should raise a ValueError. If Y is 0, then convert should raise a ZeroDivisionError. gauge expects an int and returns a str that is: "E" if that int is less than or equal to 1, "F" if that int is greater than or equal to 99, and "Z%" otherwise, wherein Z is that same int.