-1

:( correct fuel.py passes all test_fuel checks expected exit code 0, not 1

when testing my code, which then prevents the following tests from running

Here is my code for fuel.py:

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


def convert(fraction):
    while True:
        try:
            numerator = int(fraction.split("/")[0])
            denominator = int(fraction.split("/")[1])
            percent = float(numerator / denominator)
            if percent > 1:
                fraction = input("Fraction:")
                continue
        except (ValueError, ZeroDivisionError):
            raise
        else:
             return percent


def gauge(percent):
    if percent >= 0.99:
        return "F"
    elif percent <= 0.01:
        return "E"
    else:
        return f"{percent *100:.0f}%"


if __name__ == "__main__":
    main()

I know this code is 'good' as it passes the original fuel.py tests

My fuel_test.py file looks like this right now:

import pytest
from fuel import convert, gauge

def test_type():
    assert type(convert("1/2")) is float

def test_values():
    assert gauge(0.01) == "E"
    assert gauge(0.99) == "F"

def test_errors():
    with pytest.raises(ValueError):
        convert('value/error')
    with pytest.raises(ZeroDivisionError):
        convert('1/0')

It passes when I run pytest in terminal, so Im at a loss for what is happening here. Any help would be appreciated. THX.

1
  • be specific and clear what did you run the yielded an error, was the a result of check50 or running your code by hand? is the failure referring to a failure of your test harness or a failure to run your actual program? It sounds like the actual code failed. But I'm not sure how that's preventing your tests from running, it is perhaps preventing them from being evaluated.
    – UpAndAdam
    Commented Sep 15, 2023 at 17:13

1 Answer 1

-1

Don't return the result from gauge in main. Return nothing unless there was an error. Print the return from gauge. Most likely this is your issue. Notice that the instructions do not instruct you to have main return anything specific, this means that you should only be returning a non zero value from main in case of error. Even the notice suggests this:

Take care to return, not print, an int in convert and a str in gauge. Only main should call print.

Your main didn't call print, you just returned what you should have printed.

Also the while loop in your convert function is completely unneeded and accomplishes nothing. Additionally your division looks wrong percent = float(numerator/denominator) is casting the result of a integer division to a float value. But you need floating point division.

percent = float(numerator) / float(denominator)

1
  • and what pray tell is wrong about this answer? if you are going to downvote, say whats wrong about it
    – UpAndAdam
    Commented Sep 18, 2023 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .