I'm in need of help - Arithmetic Formatter

My code seems to be having some issues that I don’t know how to fix… any help is appreciated!

My code:

def arithmetic_arranger(problems, opt=False):
    
    # Creating dictonaries
    firstnumber = list()
    operator = list()
    lastnumber = list()
    
    # Checking number of problems
    if len(problems) > 5 :
        print ("Error: Too many problems.")

    for problem in problems:
        # Checking number of problems
        if len(problems) >= 6 :
            print ("Error: Too many problems.")
            continue
        # Find first number space
        ffnspace = problem.find(" ")
        intffnspace = int(ffnspace)
        ffn = problem[0:intffnspace]
        try:
            intffn = int(ffn)
            firstnumber.append(intffn)
        except:
            print("\n", "Error: Numbers must only contain digits.")
            continue
        # find second number space
        fsnspace = ffnspace + 3
        intfsnspace = int(fsnspace)
        fsn = problem[fsnspace:]
        try:
          intfsn = int(fsn)
          lastnumber.append(intfsn)
        except:
          print("\n", "Error: Numbers must only contain digits.")
          continue
        # finding operator
        fospace = ffnspace + 1
        intfo = int(fospace)
        fo = problem[intfo]
        operator.append(fo)

    def arranged_problems():
      count = 0
      for number in firstnumber:
          newline = str(firstnumber[count]).rjust(5)
          count = count + 1
          print(newline, end="    ")

      count = 0
      for op in operator:
          newline = operator[count]
          count = count + 1
          if count == 4:
              print(newline, end="")
              print(str(lastnumber[count - 1]).rjust(4), end="")
          elif count == 3:
              print(newline, end="")
              print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
          elif count == 2:
              print(newline, end="")
              print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
          elif count == 1:
              print("\n", end="")
              newnewline = newline.ljust(2)
              print(newnewline, end="")
              print(str(lastnumber[count - 1]).rjust(3), end=" "*4)
    
      count = 0
      for number in firstnumber:
          newline = "-----"
          count = count + 1
          if count == 1:
              print("\n", end="")
              print(newline, end="    ")
          else:
              print(newline, end="    ")


    return arranged_problems()

The output is the following:

 python main.py
   32     3801       45      123    
+ 698    -   2    +  43    +  49
-----    -----    -----    -----    None
    3     3801       45      123    
+ 855    -   2    +  43    +  49
F-----    -----    -----    -----        3     3801       45      123    
/ 855    -   2    +  43    +  49
F-----    -----    -----    -----    
 Error: Numbers must only contain digits.
   98     3801       45      123    
-   2    +  43    +  49    
F-----    -----    -----    -----       32        1       45      123    
- 698    -3801    +  43    +  49
F-----    -----    -----    -----       24     3801       45      123    
+ 85215    -   2    +  43    +  49
F-----    -----    -----    -----    Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
F
======================================================================
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"]

======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 24, in test_incorrect_operator
    self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: None != "Error: Operator must be '+' or '-'." : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 34, in test_only_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: None != 'Error: Numbers must only contain digits.' : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/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 arithemetic problems and a second argument of `True`.')
AssertionError: None != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 29, in test_too_many_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
AssertionError: None != 'Error: Numbers cannot be more than four digits.' : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 19, in test_too_many_problems
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: None != 'Error: Too many problems.' : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."

----------------------------------------------------------------------
Ran 6 tests in 0.066s

FAILED (failures=6)

it seems your function does not return what’s required, make sure to use the return keyword to determine the output

That makes sense I’ll try adding that to my code, but still, there should be other problems that have the right output…

how many return statements are in your code?

My updated code is like this:

def arithmetic_arranger(problems, opt=False):
    
    # Creating dictonaries
    firstnumber = list()
    operator = list()
    lastnumber = list()
    error = 0

    for problem in problems:
        # Checking number of problems
        if len(problems) >= 6 :
            error =1
            return ("Error: Too many problems.")
            continue
        # Find first number space
        ffnspace = problem.find(" ")
        intffnspace = int(ffnspace)
        ffn = problem[0:intffnspace]
        try:
            intffn = int(ffn)
            firstnumber.append(intffn)
        except:
            error =1
            return ("Error: Numbers must only contain digits.")
            continue
        # find second number space
        fsnspace = ffnspace + 3
        intfsnspace = int(fsnspace)
        fsn = problem[fsnspace:]
        try:
          intfsn = int(fsn)
          lastnumber.append(intfsn)
        except:
          error =1
          return ("Error: Numbers must only contain digits.")
          continue
        # finding operator
        fospace = ffnspace + 1
        intfo = int(fospace)
        fo = problem[intfo]       
        if fo == "+":
            operator.append(fo)   
        elif fo == "-":
            operator.append(fo)        
        else:
            error =1
            return ("Error: Operator must be '+' or '-'.")
            continue

    def arranged_problems():
      if error == 0:
        count = 0
        for number in firstnumber:
            newline = str(firstnumber[count]).rjust(5)
            count = count + 1
            print(newline, end="    ")

        count = 0
        for op in operator:
            newline = operator[count]
            count = count + 1
            if count == 4:
                print(newline, end="")
                print(str(lastnumber[count - 1]).rjust(4), end="")
            elif count == 3:
                print(newline, end="")
                print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
            elif count == 2:
                print(newline, end="")
                print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
            elif count == 1:
                print("\n", end="")
                newnewline = newline.ljust(2)
                print(newnewline, end="")
                print(str(lastnumber[count - 1]).rjust(3), end=" "*4)
    
        count = 0
        for number in firstnumber:
            newline = "-----"
            count = count + 1
            if count == 1:
                print("\n", end="")
                print(newline, end="    ")
            else:
                print(newline, end="    ")
        
        if opt == True:
            count = 0
            for number in firstnumber:
                newline = str(firstnumber[count] + lastnumber[count]).rjust(5)
                count = count + 1
                print(newline, end="    ")

    return arranged_problems()

And the output is this:

 python main.py
   32     3801       45      123    
+ 698    -   2    +  43    +  49
-----    -----    -----    -----    None
    3     3801       45      123    
+ 855    -   2    +  43    +  49
F..-----    -----    -----    -----       32        1       45      123    
- 698    -3801    +  43    +  49
F-----    -----    -----    -----      730     3802       88      172       24     3801       45      123    
+ 85215    -   2    +  43    +  49
F.
======================================================================
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"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/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 arithemetic problems and a second argument of `True`.')
AssertionError: None != '   32         1      45      123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 29, in test_too_many_digits
    self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
AssertionError: None != 'Error: Numbers cannot be more than four digits.' : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."

----------------------------------------------------------------------
Ran 6 tests in 0.002s

FAILED (failures=3)

you need to also return the arranged numbers, not just print

also, at this point I don’t think you need the continue after the return as a return exit from the function

I’ve done what you said about the “continue”, thanks :slight_smile:
But I still don’t know how I would go around to returning the arranged numbers…

you neednto create a string and return it. Maybe everytime you print you could instead concatenate that to a string, and return as last thing

I’ll try that, thank you so much for all the help :smiley:, I was really lost…

Okay, I tried your suggestion to concatenate and what I’m doing is first turning what I was printing into a string and then concatenating that string into a universal string in the arithmetic_arranger.py, but I’m getting an error.
My code:

line = str(newline, end="    ")

The output:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
  File "/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py", line 92, in arithmetic_arranger
    return arranged_problems()
  File "/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py", line 53, in arranged_problems
    line = str(newline, end="    ")
TypeError: 'end' is an invalid keyword argument for str()

that is not valid python, you can use + to concatenate two strings

to see what keywords can be used with str you can take a look at the documentation

Ok, I’ll probably just rebuild this whole part…

it’s not necessary

you can do `line = str + " "

Ok, I have a couple of ideas that I’m going to test out