Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

CS50 Python pset5 Refueling ValueError

$
0
0

I am trying to pass the check50 test on test_fuel problem and am stuck with one last frown:check50 output. Here is my code for test_fuel.py:

import fuelimport pytestdef test_convert():    assert fuel.convert("1/4") == 25    assert fuel.convert("1/2") == 50def test_gauge():    assert fuel.gauge(1) == "E"    assert fuel.gauge(30) == "30%"    assert fuel.gauge(99) == "F"def test_zerodivision():    with pytest.raises(ZeroDivisionError):        fuel.convert("1/0")def test_valueerror():    with pytest.raises(ValueError):        fuel.convert("2/1")        fuel.convert("cat/cat")        fuel.convert('cat/dog')        fuel.convert("2.35")        fuel.convert(2,375)        fuel.convert("0")

and that is my fuel.py :

def main():    flevel = input("Enter Fraction: ")    perc = convert(flevel)    print(gauge(perc))def convert(fraction):    try:        x,y = fraction.split('/')        x = int(x)        y = int(y)        if x > y:            raise ValueError        z = x/y        if z <= 1:            return z * 100    except(ValueError):        pass    except(ZeroDivisionError):        passdef gauge(percentage):    if percentage <= 1:        return "E"    elif percentage >= 99:        return "F"    else:        return f"{percentage:,.0f}%"if __name__ == "__main__":    main()

Tried to move while loop inside main() function because i dont like that when it is inside convert i can only prompt once for a fraction. Added raise ValueError when x>y to detects fractions larger than 1 but test still didnt pass check50


Viewing all articles
Browse latest Browse all 23131

Trending Articles