# Help...Arithmetic Formatter

I’ve been trying to solve this for weeks.
Been reading from the forum & watching videos but I can’t get it right.

I don’t know exactly how to read the error message on replit but it seemed I got an extra line in my output.

``````import re

def arithmetic_arranger(problems, check = False):
if len(problems) > 5:
return 'Error: Too many problems'

top = ''
bottom = ''
dashes = ''
result = ''
output = ''

for i in problems:
if i == re.search('[a-zA-Z]', i):
return 'Error: Numbers must only contain digits'

pie = i.split()
info1 = int(pie[0])
info2 = int(pie[2])
if len(pie[0]) > 4 or len(pie[2]) > 4:
return 'Error: Numbers cannot be more than four digits'

if pie[1] == '+':
num = info1 + info2
elif pie[1] == '-':
num = info1 - info2
else:
return 'Error: Operator must be '+' or '-''

if len(str(num)) == max(len(pie[0]), len(pie[2]), len(str(num))):
space = len(str(num))
elif len(pie[0]) >= len(pie[2]):
space = len(pie[0])
else:
space = len(pie[2])

width = space + 2

line1 = str(info1).rjust(width)
line2 = pie[1] + str(info2).rjust(width - 1)
line3 = '-'.rjust(width, '-')
line4 = str(num).rjust(width)

if i != problems[-1]:
top = line1 + '    '
bottom = line2 + '    '
dashes = line3 + '    '
result = line4
else:
top = line1
bottom = line2
dashes = line3
result = line4

if check:
output = top + '\n' + bottom + '\n' + dashes + '\n' + result
else:
output = top + '\n' + bottom + '\n' + dashes
return output

``````

My Output:

``````fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

@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 solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert '   988\n+   ...-----\n  1028' == '   32       ... 172     1028'
E         -    32         1      45      123      988
E         - - 698    - 3801    + 43    +  49    +  40
E         - -----    ------    ----    -----    -----
E         -  -666     -3800      88      172     1028
E         +    988
E         + +   40
E         + ------...
E
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
``````

You want “+=” here, because right now you are overriding top which results in it only containing one problem → if you look at the error, you indeed see that there is only the last problem displayed.

1 Like

Thanks so much! @Jagaya
That solved the format part for me.!

But my code above was not all correct.
I did some changes and now it works!

Thank you!!

Here is my final code.

``````def arithmetic_arranger(problems, check = False):
if len(problems) > 5:
return 'Error: Too many problems.'
top = ''
bottom = ''
dashes = ''
result = ''
output = ''
for i in problems:
pie = i.split()
if pie[0].isnumeric() == False:
return 'Error: Numbers must only contain digits.'
if pie[2].isnumeric() == False:
return 'Error: Numbers must only contain digits.'

info1 = int(pie[0])
info2 = int(pie[2])
if len(pie[0]) > 4 or len(pie[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'

if pie[1] == '+':
num = info1 + info2
elif pie[1] == '-':
num = info1 - info2
else:
return "Error: Operator must be '+' or '-'."

if len(pie[0]) >= len(pie[2]):
space = len(pie[0])
else:
space = len(pie[2])

width = space + 2

line1 = str(info1).rjust(width)
line2 = pie[1] + str(info2).rjust(width - 1)
line3 = '-' * width #or '-'.rjust(width, '-')
line4 = str(num).rjust(width)

if i != problems[-1]:
top += line1 + '    '
bottom += line2 + '    '
dashes += line3 + '    '
result += line4 + '    '
else:
top += line1
bottom += line2
dashes += line3
result += line4

if check:
output = top + '\n' + bottom + '\n' + dashes + '\n' + result
else:
output = top + '\n' + bottom + '\n' + dashes
return output

``````
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.