Arithmetic Formatter - Help for a Newbie!

I’m new to coding and can’t solve the errors in my program. Any advice would be greatly appreciated!

def arithmetic_arranger(problems, solve = False):
  
  import string
  import re

  first = ""
  second = ""
  lines = ""
  sumx = ""
  string = ""
    
  opertype = "\+|-"

  if (len(problems) > 5):
    return "Error: Too many problems."

  for index, problem in enumerate(problems):
    objectnum = problem.split(" ")

    firstnumber = objectnum[0]
    operator = objectnum[1]
    secondnumber = objectnum[2]

    if re.search(opertype, operator) == None:
      return "Error: Operator must be '+' or '-'."
  
    def check(problems):
      for problem in problems:
        if problem not in string.digits:
          return "Error: Numbers must only contain digits."

    if (len(firstnumber) > 4 or len(secondnumber) > 4):
      return "Error: Numbers cannot be more than four digits."

    problemlength = max(len(firstnumber), len(secondnumber))
    topline = str(firstnumber).rjust(problemlength)
    bottomline = operator + str(secondnumber).rjust(problemlength - 1)
    equationline = "-" * int(problemlength)
    result = str(sum).rjust(problemlength)

    answer = ""
    if (operator == "+"):
      answer == str(int(firstnumber) + int(secondnumber))
    elif (operator == "-"):
      answer == str(int(firstnumber) - int(secondnumber))

    if problem != problems[-1]:
      first += topline + "    "
      second += bottomline + "    "
      lines += equationline + "    "
      sumx += result + "    "
    else:
      first += topline
      second += bottomline
      lines += equationline
      sumx += result

  if solve:
    string = first + "\n" + second + "\n" + lines + "\n" + sumx
  else:
    string = first + "\n" + second + "\n" + lines
  return string

It really, really help if you tell us what errors you are seeing :smiley:


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 (’).

I get it. I’m having trouble solving this one too. I hope someone responds! Good luck.
Susa

+...80\n-    ----' == '  1         ...---    ------'
E         -   1         1
E         ? -- --
E         + 1       1
E         - + 2    - 9380
E         ?  -      -
E         + +2    -9380
E         - ---    ------...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
________________ test_template[test_four_problems_arrangement] _________________

arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "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 ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
E       assert '  3    3801 ...    --    ---' == '    3      3...----    -----'
E         -     3      3801      45      123
E         ? --   --            --  --
E         +   3    3801    45    123
E         - + 855    -    2    + 43    +  49
E         ?  -          --      -       --
E         + +855    -  2    +43    +49
E         - -----    ------    ----    -----...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
________________ test_template[test_five_problems_arrangement] _________________

arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

    @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 ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E       assert '11    3801  ...  ---    ----' == '  11      38...---    ------'
E         -   11      3801      1      123         1
E         ? --  --            -- --              --
E         + 11    3801    1    123       1
E         - +  4    - 2999    + 2    +  49    - 9380
E         ?  --      -         -      --       -
E         + +4    -2999    +2    +49    -9380
E         - ----    ------    ---    -----    ------...
E         
E         ...Full output truncated (2 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
_______________________ test_template[test_only_digits] ________________________

arguments = [['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers must only contain digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

problems = ['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49'], solve = False

    def arithmetic_arranger(problems, solve = False):
    
      import string
      import re
    
      first = ""
      second = ""
      lines = ""
      sumx = ""
      string = ""
    
      opertype = "\+|-"
    
      if (len(problems) > 5):
        return "Error: Too many problems."
    
      for index, problem in enumerate(problems):
        objectnum = problem.split(" ")
    
        firstnumber = objectnum[0]
        operator = objectnum[1]
        secondnumber = objectnum[2]
    
        if re.search(opertype, operator) == None:
          return "Error: Operator must be '+' or '-'."
    
        def check(problems):
          for problem in problems:
            if problem not in string.digits:
              return "Error: Numbers must only contain digits."
    
        if (len(firstnumber) > 4 or len(secondnumber) > 4):
          return "Error: Numbers cannot be more than four digits."
    
        problemlength = max(len(firstnumber), len(secondnumber))
        topline = str(firstnumber).rjust(problemlength)
        bottomline = operator + str(secondnumber).rjust(problemlength - 1)
        equationline = "-" * int(problemlength)
        result = str(sum).rjust(problemlength)
    
        answer = ""
        if (operator == "+"):
>         answer == str(int(firstnumber) + int(secondnumber))
E         ValueError: invalid literal for int() with base 10: '3g5'

arithmetic_arranger.py:43: ValueError
_______________ test_template[test_two_problems_with_solutions] ________________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] 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 ["3 + 855", "988 + 40"] and a second argument of `True`.
E       assert '  3    988\n...function sum>' == '    3      9... 858     1028'
E         -     3      988
E         ? --   --
E         +   3    988
E         - + 855    +  40
E         ?  -        --
E         + +855    +40
E         - -----    -----...
E         
E         ...Full output truncated (4 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
_______________ test_template[test_five_problems_with_solutions] _______________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
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 ' 32       1 ...function sum>' == '   32       ... 172     1028'
E         -    32         1      45      123      988
E         ? --          -- --      --       --
E         +  32       1    45    123    988
E         - - 698    - 3801    + 43    +  49    +  40
E         ?  -        -         -       --       --
E         + -698    -3801    +43    +49    +40
E         - -----    ------    ----    -----    -----...
E         
E         ...Full output truncated (4 lines hidden), use '-vv' to show

test_module.py:77: AssertionError
=========================== short test summary info ============================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - Assert...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - Assert...
FAILED test_module.py::test_template[test_four_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_five_problems_arrangement] - Assert...
FAILED test_module.py::test_template[test_only_digits] - ValueError: invalid ...
FAILED test_module.py::test_template[test_two_problems_with_solutions] - Asse...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - Ass...
========================= 7 failed, 3 passed in 0.33s ===

Thank you, Jeremy! I appreciate your prompt response. I have pasted the error messages I am receiving.

Thank you, Susan! Nice to know someone else is having difficulty with this code too.

It looks like your spacing doesn’t match the requirements