# Aritmetic formatter

Tell us what’s happening:
Describe your issue in detail here. I am getting errors in this code and i cannot figure it out

``````def arithmetic_arranger(problems, answer = False):
#too many problems
if len(problems)>5:
return"Error: Too many problems."
line1 = ""
line2 = ""
line3 = ""
line4 = ""
for x,problem in enumerate (problems):
op1,operator,op2 = problem.split()
#operators accepted
if operator not in ['+','-']:
return"Operator must be '+' or '-'."
#digits in numbers
if len(op1) > 4 or len(op2) > 4 :
return"Error: Numbers cannot be more than four digits."
#operand number only check
if not op1.isdigit() or op2.isdigit():
return"Error: Numbers must only contain digits."
spacing = max(len(op1),len(op2))
result =int(op1) + int(op2) if operator =='+' else int(op1) - int(op2)

#arrangment format
line1 = line1 + op1.rjust(spacing)
line2 = line2 + operator +op2.rjust(spacing+1)
line3 = line3 + ''.rjust(spacing+2,'-')
line4 = line4 + str(result).rjust(spacing+2)

if x < len(problems) - 1:
line1 += ' '
line2 += ' '
line3 += ' '
line4 += ' '
#arg
arranged_problems = line1 + '\n' + line2 + '\n' + line3
else :
arranged_problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4

return arranged_problems

``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36`.

Challenge: Arithmetic Formatter

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Please tell us what the error you are seeing is.

this is error i am getting sorry for replying late my pc took long time too bootup

``````FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-6/test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: 'Error: Numbers must only contain digits.' != '   32         1      45      123\n- 698  [89 chars] 172'
- Error: Numbers must only contain digits.
+    32         1      45      123
- 698    - 3801    + 43    +  49
-----    ------    ----    -----
-666     -3800      88      172
: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.007s

FAILED (failures=3, errors=1)
``````

Hi , i will try to help.
Here you should put “not” for op2.isdigit() as well (right after “or”) because otherwise you only check for op1 and every time op2 IS a number the func returns the ‘Error: Numbers must only contain digits’…

That’s why you get:

This means that the func returned Error: Numbers must only contain digits. instead of 32 1 45 123\n- 698 [89 chars] 172

Also the spacing for the first line is wrong but you’ll figure it out!!

Hi Leon1 i apologize for replying late, after putting not in front op2 iam getting an error like this

``````self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.')
AssertionError: ' 32    1 45 123\n- 698 - 3801 + 43 +  49\n[44 chars] 172' != '   32         1      45      123\n- 698   [88 chars] 172'
-  32    1 45 123
+    32         1      45      123
- - 698 - 3801 + 43 +  49
+ - 698    - 3801    + 43    +  49
?       +++       +++     +++
- ----- ------ ---- -----
+ -----    ------    ----    -----
?      +++        +++    +++
-  -666  -3800   88   172+  -666     -3800      88      172?      +++          +++  +++
: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithmetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.005s
``````

and quite did not understand the spacing bit

Is this the only error left? You’re almost there!
Try looking at it this way:
(lines with - are your output, + is the desired outcome and ? are the differences)

``````-  32    1 45 123
+    32         1      45      123
- - 698 - 3801 + 43 +  49
+ - 698    - 3801    + 43    +  49
?       +++       +++     +++
- ----- ------ ---- -----
+ -----    ------    ----    -----
?      +++        +++    +++
-  -666  -3800   88   172+  -666     -3800      88      172?      +++          +++  +++
``````

It looks like there are the same amount of spaces missing on each line.
Is it more clear like this?

well i was able to run the code succesfully thanks everyone for your help .this is my final code.

``````def arithmetic_arranger(problems,answer = False):
#too many problems
if len(problems)>5:
return"Error: Too many problems."
line1 = ""
line2 = ""
line3 = ""
line4 = ""
for x,problem in enumerate (problems):
op1,operator,op2 = problem.split()
#operators accepted
if operator not in ['+','-']:
return"Error: Operator must be '+' or '-'."
#digits in numbers
if len(op1) > 4 or len(op2) > 4 :
return"Error: Numbers cannot be more than four digits."
#operand number only check
if not op1.isdigit() or not op2.isdigit():
return"Error: Numbers must only contain digits."
spacing = max(len(op1),len(op2))
result =int(op1) + int(op2) if operator =='+' else int(op1) - int(op2)

#arrangment format
line1 = line1 + op1.rjust(spacing+2)
line2 = line2 + operator.ljust(2) +op2.rjust(spacing)
line3 = line3 + ''.rjust(spacing+2,'-')
line4 = line4 + str(result).rjust(spacing+2)

if x < len(problems) - 1:
line1 += '    '
line2 += '    '
line3 += '    '
line4 += '    '
#arg
arranged_problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4
else :
arranged_problems = line1 + '\n' + line2 + '\n' + line3

return arranged_problems
``````
1 Like

Well done, this is definitely a tricky one that seems to give a lot of people trouble. For reference, here’s my (slightly over-engineered) solution:

``````def arithmetic_arranger(problems, display_answers=False):
if len(problems) > 5:
return "Error: Too many problems."

arithmetic_problems = []

for p in problems:
operand1, operator, operand2 = p.split()

operand_len = max(len(operand1), len(operand2))
if operand_len > 4:
return "Error: Numbers cannot be more than four digits."

try:
if operator == "+":
elif operator == "-":
else:
return "Error: Operator must be '+' or '-'."
except ValueError:
return "Error: Numbers must only contain digits."

problem_width = operand_len + 2
problem_lines = (
operand1.rjust(problem_width),
f"{operator} {operand2.rjust(operand_len)}",
"-"*problem_width,
)
arithmetic_problems.append(problem_lines)

arranged_lines = [
(" "*4).join(sections) for sections in zip(*arithmetic_problems)
]