【发布时间】: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