Arithmetic Formatter Problem Arrangement

Tell us what’s happening:
After looking at some posts related to this topic, I’ve realized that I have overcomplicated my code. I’ll try and simplify it later. At the moment, I’m having trouble with the correct formatting for the problems at the end. I can’t figure out how to print the problems side by side, and the problems themselves don’t match evenly on the right side. I’ve tried a few different solutions but the second number is always out of alignment.

Your code so far

def arithmetic_arranger(problems):

  new_list = []
  stripped_list = []
  split_list = []
  nested_list = []

  #Checks the number of problems
  if len(problems) > 5:
    return "Error: Too many problems."

  #Checks for the operator
  for item in problems:
    if '+' in item:
      pass
    elif '-' in item:
      pass
    else:
      return "Error: Operator must be '+' or '-'."

  #Creates a new list with the operands minus operators
  for item in problems:
    if '+' in item:
      new_list.append(item.replace('+',''))
    else:
      new_list.append(item.replace('-',''))
  
  #Creates a stripped list with the two operands combined
  for item in new_list:
    stripped_list.append(item.replace(' ',''))

  #Checks if items in the stripped list are numeric
  for item in stripped_list:
    if item.isnumeric() == False:
      return 'Error: Numbers must only contain digits.'
    else:
      pass

  #Creates a split list where the two operands become their own element in a list
  for item in new_list:
    split_list.append(item.split())

  #Checks the length of each operand in the previously created list
  for item in split_list:
    for items in item:
      if len(items) > 4:
        return 'Error: Numbers cannot be more than four digits.'
      else:
        pass
  
  #Creats a nested list of the problems list split by whitespace
  for item in problems:
    nested_list.append(item.split())

  col_width = max(len(num) for item in nested_list for num in item) + 1

  for item in nested_list:
      print(item[0].rjust(col_width))
      print(item[1] + item[2].rjust(col_width))
      print('-' * col_width)
      
  
  #return arranged_problems

This is the output I get:

   32
+  698
-----
 3801
-    2
-----
   45
+   43
-----
  123
+   49
-----
None
    3
+  855
-----
 3801
-    2
-----
   45
+   43
-----
  123
+   49
-----
F..E..
======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 37, in test_solutions
    actual = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49"], True)
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given

======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: None != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

----------------------------------------------------------------------
Ran 6 tests in 0.004s

FAILED (failures=1, errors=1)


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Since you added an extra character ("+") you need to reduce the col_width.

Most solutions I’ve seen to this build an entire line as a string and then join and return the strings. So, instead of printing each problem as you loop through, try making a string for each line you will need to eventually print and then join them into one string and return that string. You will need to return instead of print() to pass the tests.

you need to return the output, not comment it out

Thanks for the help. Such a simple thing fixed the alignment issue.

Thank you for the hint. I will try it out.

Yes, of course. I just commented it out because I wanted to see the output of the print statements to see if it would arrange properly.