我的CS50问题集3的作业为什么只在一个参数上失败?

huangapple go评论82阅读模式
英文:

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

问题

在CS50的Python Problem Set 3中,燃油计量表,我的代码通过了除一个之外的所有测试。根据反馈,"2/3"返回"66%\n",这是不正确的。当我运行脚本时,它返回"66%",没有"\n"或新行。

所有其他分数都通过了测试,尽管它们的格式相同。有人能解释一下出了什么问题吗?(我只能假设与四舍五入有关,因为其他四舍五入的分数,如"1/3",都被四舍五入为下一个数。)

我尝试过以不同的方式格式化打印命令,使用不同的语法,但没有解决错误。我考虑过在将其更改为整数之前将答案作为浮点数获得,但这似乎是荒谬的。 代码如下:

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): # 检查输入不是字符串,分母不是0。

    while True:

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

        except (ValueError, ZeroDivisionError):
            pass

main()

评估结果如下:

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
英文:

In CS50's Python Problem Set 3, Fuel Gauge, my code passes all the tests except one. According to the feedback, &quot;2/3&quot; returns &quot;66%\n&quot;, which is incorrect. When I run the script, it returns &quot;66%&quot; without the &quot;\n&quot; 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 &quot;1/3&quot; 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(&quot;Fraction: &quot;)

    if fuel &gt;= 0 and fuel &lt;= 1:
        print(&quot;E&quot;)

    if fuel &gt;= 99 and fuel &lt;= 100:
        print(&quot;F&quot;)

    if fuel &gt; 1 and fuel &lt;99:
        print(f&quot;{fuel}%&quot;)

    else:
        if fuel &lt; 0 or fuel &gt; 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 &quot;67%&quot;, not &quot;66%\n&quot;
:) 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

答案1

得分: 0

问题在于你在get_fraction()函数中使用int()将分数转换为整数,但没有对分数进行四舍五入。如果使用int(),所有小数位将被截断,换句话说,它将被向下舍入。相反,尝试使用round()来四舍五入分数。

from fractions import Fraction as frac

def main():
    fuel = get_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): # 检查输入不是字符串且分母不为0。
    while True:
        try:
            fuel = input(prompt)
            return round(frac(fuel) * 100)
        except (ValueError, ZeroDivisionError):
            pass

main()

此外,在你的main()函数中,你应该使用elif语句来处理额外的情况,而不是反复使用if,但这不影响问题。

英文:

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(&quot;Fraction: &quot;)
    if fuel &gt;= 0 and fuel &lt;= 1:
        print(&quot;E&quot;)
    elif fuel &gt;= 99 and fuel &lt;= 100:
        print(&quot;F&quot;)
    elif fuel &gt; 1 and fuel &lt;99:
        print(f&quot;{fuel}%&quot;)
    else:
        if fuel &lt; 0 or fuel &gt; 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.

huangapple
  • 本文由 发表于 2023年1月9日 01:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定