我的输出格式与FreeCodeCamp Python挑战测试的格式不匹配



我正在尝试完成一个FreeCodeCamp课程,其中包括为一个项目创建一个程序。这个程序包括以某种方式格式化算术问题;我觉得详细描述项目并不太重要,因为错误源于格式。他们的期望可以在错误消息中找到。我的输出看起来几乎与项目所需的预期输出相同,但我不知道为什么它说我的代码不正确。我的代码工作得很好,但它并不完全符合预期…我觉得我是在浪费时间,所以我向你们求助。我试着破译他们的错误信息——有人能解释一下这条信息实际上描述的是什么错误,以及我如何修复它吗?如果您需要进一步的信息,请告诉我,我可以提供给您。

我代码:

def arithmetic_arranger(problems, solve=False):
if len(problems) > 5:
return "Error: Too many problems."
top_numbers = ''
bottom_numbers = ''
dashes = ''
solutions = ''
for problem in problems:
split_terms = problem.split(' ')
equation_length = max([(len(x)+2) for x in split_terms])
term_1_length = len(split_terms[0])
term_2_length = len(split_terms[2])
if len(split_terms) != 3:
return 'Please format probems as "x + y" with spaces between numbers and operators'
if split_terms[1] not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
# if type(split_terms[0]) != int or type(split_terms[2]) != int:
try:
int(split_terms[0])
int(split_terms[2])
except:
return "Error: Numbers must only contain digits." 
if term_1_length > 4 or term_2_length > 4:
return "Error: Numbers cannot be more than four digits."
solution = str(eval(problem))
solution_length = len(str(solution))
# if len(split_terms[0]) < len(split_terms[2]):
whitespaces_1 = (equation_length - term_1_length) * ' '
whitespaces_2 = (equation_length - term_2_length - 1) * ' '
operator = split_terms[1]
dash_len = '-'*(equation_length)
top_numbers += whitespaces_1 + split_terms[0] + '    '
bottom_numbers += str(operator) + whitespaces_2 + split_terms[2] + '    '
dashes += dash_len + '    '
solutions += ((equation_length - solution_length) * ' ') + solution + '    '
arranged_problems = top_numbers + 'n' + bottom_numbers + 'n' + dashes
if solve == True:
arranged_problems += 'n' + solutions
return arranged_problems

我收到的错误信息:

________________ test_template[test_two_problems_arrangement1] _________________
arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123n-    2    +  49n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    n-    2    +  49    n------    -----    ' == '  3801      123n-    2    +  49n------    -----'
E         -   3801      123↔
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         + -    2    +  49    
E         ?                ++++
E         - ------    -----
E         + ------    -----    
E         ?                ++++
test_module.py:77: AssertionError

他们期望的输出是:

"  3801      123n-    2    +  49n------    -----"

你的实际输出是:

"  3801      123    n-    2    +  49    n------    -----    "

请注意,在n之前和字符串结束的每一行文本都包含4个空格字符。如果你删除这些,它们将匹配。

您在每个术语的末尾添加了一个多余的' '空白字符串,包括每行上的最后一个. 因此,您的输出与期望的输出不匹配,它没有多余的终止空格。

更改代码,在将结果聚合到arranged_problems之前,从每行删除终止空格,或者干脆不将空格附加在每行的最后一项之后。

解决这个问题的一个快速方法是修改如下:
arranged_problems = top_numbers + "n" + bottom_numbers + "n" + dashes

:

arranged_problems = top_numbers.rstrip() + "n" + bottom_numbers.rstrip() + "n" + dashes.rstrip()