简体   繁体   English

CS50 python pset5加油

[英]CS50 python pset5 refueling

Here is my solution to CS50 Python pset5 refueling problem.这是我对CS50 Python pset5加油问题的解决方案。 It works fine and passes all my pytest tests.它工作正常并通过了我所有的 pytest 测试。 but check50 gives me error ":( correct fuel.py passes all test_fuel checks expected exit code 0, not 1".但是 check50 给了我错误“:(正确的fuel.py通过了所有test_fuel检查预期的退出代码0,而不是1”。

def main():
    while True:
        try:
            fraction = input("Fraction: ")
            print(gauge(convert(fraction)))
            break
        except (ValueError,ZeroDivisionError):
            continue


def convert(fraction):
            a,b = fraction.split("/")
            f = int(a) / int(b)
            if int(a) <= int(b) and int(b) != 0 and f <= 1:
                percentage = int(round(f * 100))
                return percentage
            else:
                raise (ZeroDivisionError,ValueError)


def gauge(percentage):
    Z = int(percentage)
    if Z <= 1:
        return "E"
    elif Z >= 99:
        return "F"
    else:
        return f"{Z}%"


if __name__ == "__main__":
    main()

I hope you are doing well.我希望你做得很好。 For the Unit Tests section, the course looks to have all computations, manipulations, etc. performed outside of def main().对于单元测试部分,该课程看起来所有计算、操作等都在 def main() 之外执行。 Essentially, def main() will only be used to call upon the other function and print the final results.本质上,def main() 将仅用于调用另一个 function 并打印最终结果。 So, def main() should only be所以, def main() 应该只

def main():

    fraction = input("Fraction: ")
    print(gauge(convert(fraction)))

This means you will have to move the while True loop to the def convert().这意味着您必须将 while True 循环移至 def convert()。

Also, the raises should be used in your test_fuel.py.此外,应该在你的 test_fuel.py 中使用加注。 It was suggested in the hint section and is linked here.在提示部分中提出了建议,并在此处链接。 https://docs.pytest.org/en/latest/how-to/assert.html#assertions-about-expected-exceptions . https://docs.pytest.org/en/latest/how-to/assert.html#assertions-about-expected-exceptions

Hopefully, this helps.希望这会有所帮助。 Let me know if you run into other errors or issues.如果您遇到其他错误或问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM