【问题标题】:(CS50P) check50 returns errors to my test code, that I`m sure is correct(CS50P) check50 向我的测试代码返回错误,我确定这是正确的
【发布时间】:2022-08-21 16:57:34
【问题描述】:

我有一个“Working 9 to 5”问题的工作变体和一个测试文件,它通过了所有的断言。我很确定,我已经测试了所有内容,但 check50 仍然返回此消息:

:) working.py and test_working.py exist
:) working.py does not import libraries other than sys and re
:) working.py converts \"9 AM to 5 PM\" to \"09:00 to 17:00\"
:) working.py converts \"9:00 AM to 5:00 PM\" to \"09:00 to 17:00\"
:) working.py converts \"8 PM to 8 AM\" to \"20:00 to 08:00\"
:) working.py converts \"8:00 PM to 8:00 AM\" to \"20:00 to 08:00\"
:) working.py converts \"12 AM to 12 PM\" to \"00:00 to 12:00\"
:) working.py converts \"12:00 AM to 12:00 PM\" to \"00:00 to 12:00\"
:) working.py raises ValueError when given \"8:60 AM to 4:60 PM\"
:) working.py raises ValueError when given \"9AM to 5PM\"
:) working.py raises ValueError when given \"09:00 to 17:00\"
:) working.py raises ValueError when given \"9 AM - 5 PM\"
:) working.py raises ValueError when given \"10:7 AM - 5:1 PM\"
:) correct working.py passes all test_working checks
:( test_working.py catches working.py printing incorrect hours
    expected exit code 1, not 2
:( test_working.py catches working.py printing incorrect minutes
    expected exit code 1, not 2
:( test_working.py catches working.py not raising ValueError when user omits \" to \"
    expected exit code 1, not 2
:( test_working.py catches working.py not raising ValueError for out-of-range times
    expected exit code 1, not 2
:( test_working.py catches working.py not raising ValueError for invalid time format
    expected exit code 1, not 2

如果相关,这是我的程序的代码:

工作.py

import re
import sys


def main():
    print(convert(input(\"Hours: \")))


def convert(s):
    if input := re.fullmatch(r\"((?:[0-9][0-2]*):*(?:[0-5][0-9])* (?:[A-P]M)) to ((?:[0-9][0-2]*):*(?:[0-5][0-9])* (?:[A-P]M))\", s):
        f = input.group(1)
        sec = input.group(2)
        fi = conv(f)
        se = conv(sec)
        return f\"{fi} to {se}\"
    else:
        raise ValueError


def conv(t):
    if \"AM\" in t:
        t = t.replace(\"AM\", \"\")
        return make_right(t, \"AM\")
    elif \"PM\" in t:
        t = t.replace(\"PM\", \"\")
        return make_right(t, \"PM\")
    else:
        return None


def make_right(time, v):
    if format1 := re.search(r\"[0-9](:[0-9][0-9])\", time):
        minutes = format1.group(1)
        time = time.replace(format1.group(1), \"\")
        time = int(time)
        minutes = minutes.replace(\":\", \"\")
        if int(minutes) >= 60:
            raise ValueError
        if time == 12 and v == \"AM\":
            time = 0
        elif v == \"PM\":
            if time == 12:
                time = 12
            else:
                time = time + 12
        return f\"{time:02d}:{minutes}\"
    elif format2 := re.search(r\"[0-9]\", time):
        time = int(time)
        if time == 12 and v == \"AM\":
            time = 0
        elif v == \"PM\":
            if time == 12:
                time = 12
            else:
                time = time + 12
        return f\"{time:02d}:00\"
    else:
        return None


if __name__ == \"__main__\":
    main()

test_working.py

from working import convert
import pytest

def test_wrong_hour():
    with pytest.raises(ValueError):
        convert(\"13 AM to 5 PM\")

def test_wrong_minute():
    with pytest.raises(ValueError):
        convert(\"12:60 AM to 5 PM\")


def test_time():
    assert convert(\"9 AM to 5 PM\") == \"09:00 to 17:00\"
    assert convert(\"9:30 AM to 5:45 PM\") == \"09:30 to 17:45\"


def test_to():
    with pytest.raises(ValueError):
        convert(\"9 AM 5 PM\")


def test_format():
    with pytest.raises(ValueError):
        convert(\"9 to 5\")
    with pytest.raises(ValueError):
        convert(\"17:00 to 9 PM\")


test_wrong_hour()
test_wrong_minute()
test_time()
test_to()
test_format()

我什至尝试使用该解决方案查看 YouTube 视频。我没有逐行复制它,但在最后基本上做了和那个视频说的一样的事情。它仍然向我返回此消息,我无法弄清楚它为什么会这样。你们中有人参加过 CS50P 课程并遇到过这个问题吗?如果是这样,您知道解决方案吗?

  • 请注意,失败都与test_working.py 相关,而不是working.py
  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example

标签: python python-3.x cs50


【解决方案1】:

根据 [pytest 文档][1] 要测试一些东西,你只需要让函数以test_或_test开头,最后不需要调用函数。即使 pytest 工作正常,就像您调用该函数一样,但 check50 不喜欢那样

从你的代码中删除这部分,你应该没问题。

test_wrong_hour()
test_wrong_minute()
test_time()
test_to()
test_format()

还有,你可以把答案分享给全世界吗??? [1]:https://docs.pytest.org/en/7.1.x/

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多