# Arithmetic formatter challenge - formatting columns

HI all, I am a python beginner, trying to complete the “Arithmetic formatter” challenge as part of the “Scientific Computing with Python” but stuck on formatting my output.

My code is:

``````def arithmetic_arranger(problems: list, answers=False):
if(len(problems) > 5):
return 'Error: Too many problems.'

first_num = ''
second_num = ''
operator = ''

line_one = line_two = line_three = line_four = ''

"""1. Break-down the problems list into items"""
for i in problems:
split_item = i.split()
first_num = split_item[0]
operator = split_item[1]
second_num = split_item[2]

"""2. Check if the input values are valid"""
if(len(first_num) > 4 or len(second_num) > 4): #No more than 4 digit numbers
return 'Error: Numbers cannot be more than four digits.'
if not first_num.isnumeric() or not second_num.isnumeric(): #Check if the numbers contains digits only
return 'Error: Numbers must only contain digits.'

#Check if the operands are correct and if so format the output
if(operator == '+' or operator == '-'):

#Get the results from multiplication or substraction
if(operator == '+'):
result = int(first_num) + int(second_num)
else:
result = int(first_num) - int(second_num)

#Get the length of the lines
line_length = len(max(first_num, second_num)) + 2
line = ''
for s in range(line_length):
line += '-'

#Do the maths
if operator == '+':
result = int(first_num) + int(second_num)
elif operator == '-':
result = int(first_num) - int(second_num)

line_one += ' ' * (line_length - len(str(first_num))) + str(first_num) + ' ' * 4
line_two += operator + ' ' * (line_length - len(str(second_num)) - 1) + str(second_num) + ' ' * 4
line_three += line + ' ' * 4
line_four += ' ' * (line_length - len(str(result))) + str(result) + ' ' * 4

else:
return 'Error: Operator must be \'+\' or \'-\'.'

#If the answer is required, concatenate the numbers, lines and the answer
return line_one + '\n' + line_two + '\n' + line_three + '\n' + line_four
else:
return line_one + '\n' + line_two + '\n' + line_three```

The test errors:

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
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      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         ?            -
E         +   3801     123
E         ?               ++++
E         - -    2    +  49
E         ?            -
E         + -    2    + 49
E         ?               ++++
E         - ------    -----
E         ?               ^
E         + ------    ----
E         ?               ^^^^``````

Hi ^^
please provide a link to your code, as it is easier to read the full error messages there. Also there seems to be some formatting error in your post, as only half the code is formatted correctly.

Though you got the common issue that your lines will have 4 spaces at the end, which shouldn’t be there. Because you add 4 after every problem, including the last → but there shouldn’t be some after the final problem.

Hi, thank you for your reply. This is the source code https://replit.com/@pazitron1/boilerplate-arithmetic-formatter#arithmetic_arranger.py

Ok I feel like something in the error is different from what I’m used to…
Anyway there is a spacing issue with your output (-) and the expected output (+). Please read the task again on how to calculate spacing for the numbers.

``````-    32         1      45      123      988
?                        -
+    32         1      45     123      988
- - 698    - 3801    + 43    +  49    +  40
?                             -
+ - 698    - 3801    + 43    + 49    +  40
``````

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