pythoncs50

check for refuel pset5 fails


I am doing refuel, a problem set in CS50p; I have restructured my fuel.py code as instructed and it has passed check 50. unfortunately my test_fuel.py code seems to fail check50 but it passed 100% when I check it as per the problem set instructions. can someone tell me what am I doing wrong?

Here is my test_fuel.py code

from fuel import convert, gauge
import pytest


def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        convert("2/0")

def test_value_error():
    with pytest.raises(ValueError):
        convert("cat/cat")


def test_convert():
    assert convert("1/2") = 50
    assert convert("1/100") == 1
    assert convert("99/100") == 99

def test_gauge():
    gauge(50) == "50%"
    gauge(1) == "E"
    gauge(99) == "F"

and Here is my fuel.py code, the code that I am testing.

def main():
    fraction = input("Fraction: ")
    f = convert(fraction)
    percent = gauge(f)
    print(percent)




def convert(fraction):
    while True:
        try:
            numerator, denominator = fraction.split("/")
            denominator = int(denominator)
            numerator = int(numerator)
            percent = int(numerator / denominator * 100)
            if percent <= 100:
                return percent

            else:
                fraction = input("Fraction: ")
                pass

        except (ZeroDivisionError,ValueError):
            raise



def gauge(percent):
    # tank is full
    if percent >= 99:
        return "F"
    # tank is empty
    elif percent <= 1:
        return "E"
    # tank is not full or empty
    else:
        return str(percent) + "%"


if __name__ == "__main__":
    main()

I wanted my test_fuel.py to pass check50; but it only pass when I test it as per the problem set instructions.


Solution

  • The code you posted doesn't include the gauge() function. However, I noticed 3 errors without running your code.

    The 1st error is with these 2 asserts:

    assert fuel.convert("1/2") == 1 / 2
    assert fuel.convert("3/4") == 3 / 4
    

    Review the instructions. They say: the convert() function is supposed to "return the fraction as a percentage rounded to the nearest integer value between 0 and 100". So it should be:

    assert fuel.convert("1/2") == 50
    assert fuel.convert("3/4") == 75
    

    The 2nd error is with the gauge() functions.

    assert fuel.gauge(1/2) == "50%"
    assert fuel.gauge(99/100) == "F"
    assert fuel.gauge(0/100) == "E"
    

    The instructions say: "gauge expects an integer and returns a string" So these should be:

    assert fuel.gauge(50) == "50%"
    assert fuel.gauge(99) == "F"
    assert fuel.gauge(0) == "E"
    

    Finally, I noticed your convert function has return fraction_. I'm pretty sure fraction_ is a float and not an integer.

    Fix those 3 times and that should get you close.