Why does my assignment for CS50 Problem Set 3 fail only on one parameter?

Question:

In CS50’s Python Problem Set 3, Fuel Gauge, my code passes all the tests except one. According to the feedback, "2/3" returns "66%n", which is incorrect. When I run the script, it returns "66%" without the "n" or a new line.

All the other fractions pass the test, although they are formatted the same way. Could someone please explain what is going wrong? (I can only assume that it has something to do with rounding up as the other fractions that round "1/3" is rounded down.)

I have tried to format the print command differently, using different syntax, but it does not remove the error. I have thought of getting the answer as a float before changing it to an integer, but that seems ridiculous. The code is:

from fractions import Fraction as frac


def main():

    fuel = get_fraction("Fraction: ")

    if fuel >= 0 and fuel <= 1:
        print("E")

    if fuel >= 99 and fuel <= 100:
        print("F")

    if fuel > 1 and fuel <99:
        print(f"{fuel}%")

    else:
        if fuel < 0 or fuel > 100:
            main()


def get_fraction(prompt): # Checks that the input is not a string and that the denominator is not 0.

    while True:

        try:
            fuel = input(prompt)
            return int(frac(fuel) * 100)

        except (ValueError, ZeroDivisionError):
            pass


main()

The assessment is:

Results for cs50/problems/2022/python/fuel generated by check50 v3.3.7
:) fuel.py exists
:) input of 3/4 yields output of 75%
:) input of 1/3 yields output of 33%
:( input of 2/3 yields output of 67%
    expected "67%", not "66%n"
:) input of 0/100 yields output of E
:) input of 1/100 yields output of E
:) input of 100/100 yields output of F
:) input of 99/100 yields output of F
:) input of 100/0 results in reprompt
:) input of 10/3 results in reprompt
:) input of three/four results in reprompt
:) input of 1.5/4 results in reprompt
:) input of 3/5.5 results in reprompt
:) input of 5-10 results in reprompt
Asked By: RedHorseMane

||

Answers:

The issue is that you are casting a fraction to an integer with int(), but you aren’t rounding the fraction (this is in your get_fraction() function). If you use int(), all decimal places will be truncated, or in other words, it will be rounded down. Instead, try using round() to round the fraction.

from fractions import Fraction as frac


def main():
    fuel = get_fraction("Fraction: ")
    if fuel >= 0 and fuel <= 1:
        print("E")
    elif fuel >= 99 and fuel <= 100:
        print("F")
    elif fuel > 1 and fuel <99:
        print(f"{fuel}%")
    else:
        if fuel < 0 or fuel > 100:
            main()


def get_fraction(prompt): # Checks that the input is not a string and that the denominator is not 0.
    while True:
        try:
            fuel = input(prompt)
            return round(frac(fuel) * 100)
        except (ValueError, ZeroDivisionError):
            pass


main()

Also, in your main() function, you should be using elif statements for extra cases instead of repeatedly using if, but this doesn’t affect the problem.

Answered By: Michael M.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.