Scientific Computing with Python Projects - Arithmetic Formatter, need help

Hey guys, I dont have a clue why my code only fails in the test.

I know it is long, but I thought it should work at least.

Here is the link: boilerplate-arithmetic-formatter - Replit

And also here:

def arithmetic_arranger(arguments, solution=False):
  if solution:
    tp = []
    for word in arguments:
      sl = word.split()
      tp.append(sl)

    for _ in tp:
      if len(arguments) < 6:
        continue
      else:
        print("Error: Too many problems.", end="")
        exit()

    st = 0
    for a in tp:
      w1 = a[st]
      if w1.isnumeric() is True:
        continue
      else:
        print("Error: Numbers must only contain digits.", end="")
        exit()

    st3 = 2
    for a in tp:
      w3 = a[st3]
      if w3.isnumeric() is True:
        continue
      else:
        print("Error: Numbers must only contain digits.", end="")
        exit()

    st = 1
    mns = "-"
    pls = "+"
    for a in tp:
      w2 = a[st]
      if w2 == mns or w2 == pls:
        continue
      else:
        print("Error: Operator must be '+' or '-'.", end="")
        exit()

    st = 0
    for a in tp:
      w1 = a[st]
      if len(w1) < 5:
        continue
      else:
        print("Error: Numbers cannot be more than four digits.", end="")
        exit()

    st = 2
    for a in tp:
      w3 = a[st]
      if len(w3) < 5:
        continue
      else:
        print("Error: Numbers cannot be more than four digits.", end="")
        exit()

    temp = []
    for word in arguments:
      sl = word.split()
      temp.append(sl)

    stl1 = 0
    stl2 = 1
    stl3 = 2
    pov = []
    n = 0
    for b in temp:
      wrd1 = b[stl1]
      wrd2 = b[stl2]
      wrd3 = b[stl3]
      n += 1
      if n == len(temp):
        if len(wrd1) > len(wrd3):
          qi = len(wrd1) - len(wrd3)
          abst = " " * qi
          pov.append(wrd2 + ' ' + abst + wrd3)
        elif len(wrd1) == len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
        elif len(wrd1) < len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
      else:
        if len(wrd1) > len(wrd3):
          qi = len(wrd1) - len(wrd3)
          abst = " " * qi
          pov.append(wrd2 + ' ' + abst + wrd3)
        elif len(wrd1) == len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
        elif len(wrd1) < len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)

    y = " " * 4
    stl = 0
    n = 0
    for a, los in zip(temp, pov):
      wrd1 = a[stl]
      n += 1
      if n == len(temp):
        if len(los) == 6:
          print(f"{wrd1: >10}", "\n", end="")
        if len(los) == 5:
          print(f"{wrd1: >9}", "\n", end="")
        if len(los) == 4:
          print(f"{wrd1: >8}", "\n", end="")
        if len(los) == 3:
          print(f"{wrd1: >7}", "\n", end="")
      elif n == 1:
        if len(los) == 6:
          print(f"{wrd1: >6}", end="")
        if len(los) == 5:
          print(f"{wrd1: >5}", end="")
        if len(los) == 4:
          print(f"{wrd1: >4}", end="")
        if len(los) == 3:
          print(f"{wrd1: >3}", end="")
      else:
        if len(los) == 6:
          print(f"{wrd1: >10}", end="")
        if len(los) == 5:
          print(f"{wrd1: >9}", end="")
        if len(los) == 4:
          print(f"{wrd1: >8}", end="")
        if len(los) == 3:
          print(f"{wrd1: >7}", end="")

    stl1 = 0
    stl2 = 1
    stl3 = 2
    g = []
    n = 0
    for b in temp:
      wrd1 = b[stl1]
      wrd2 = b[stl2]
      wrd3 = b[stl3]
      n += 1
      if n == len(temp):
        if len(wrd1) > len(wrd3):
          z = len(wrd1) - len(wrd3)
          abst = " " * z
          g.append(wrd2 + ' ' + abst + wrd3)
          print(f"{wrd2 + ' ' + abst + wrd3 + y:>}", "\n", end="")
        elif len(wrd1) == len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", "\n", end="")
        elif len(wrd1) < len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", "\n", end="")
      else:
        if len(wrd1) > len(wrd3):
          z = len(wrd1) - len(wrd3)
          abst = " " * z
          g.append(wrd2 + ' ' + abst + wrd3)
          print(f"{wrd2 + ' ' + abst + wrd3 + y:>}", end="")
        elif len(wrd1) == len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", end="")
        elif len(wrd1) < len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", end="")

    i = "-"
    n = 0
    for li in g:
      lin = len(li)
      n += 1
      print(f"{i * lin + y:>}", end="")
    print("")

    tep = []
    for d in arguments:
      sul = eval(d)
      tep.append(sul)
    lst = tep

    n = 0
    for x, ll in zip(lst, g):
      n += 1
      if n == len(temp):
        if len(ll) == 6:
          print(f"{str(x): >10}", "\n", end="")
        if len(ll) == 5:
          print(f"{str(x): >9}", "\n", end="")
        if len(ll) == 4:
          print(f"{str(x): >8}", "\n", end="")
        if len(ll) == 3:
          print(f"{str(x): >7}", "\n", end="")
      elif n == 1:
        if len(ll) == 6:
          print(f"{str(x): >6}", end="")
        if len(ll) == 5:
          print(f"{str(x): >5}", end="")
        if len(ll) == 4:
          print(f"{str(x): >4}", end="")
        if len(ll) == 3:
          print(f"{str(x): >3}", end="")
      else:
        if len(ll) == 6:
          print(f"{str(x): >10}", end="")
        if len(ll) == 5:
          print(f"{str(x): >9}", end="")
        if len(ll) == 4:
          print(f"{str(x): >8}", end="")
        if len(ll) == 3:
          print(f"{str(x): >7}", end="")

  else:
    tp = []
    for word in arguments:
      sl = word.split()
      tp.append(sl)

    for _ in tp:
      if len(arguments) < 6:
        continue
      else:
        print("Error: Too many problems.", end="")
        exit()

    st = 0
    for a in tp:
      w1 = a[st]
      if w1.isnumeric() is True:
        continue
      else:
        print("Error: Numbers must only contain digits.", end="")
        exit()

    st3 = 2
    for a in tp:
      w3 = a[st3]
      if w3.isnumeric() is True:
        continue
      else:
        print("Error: Numbers must only contain digits.", end="")
        exit()

    st = 1
    mns = "-"
    pls = "+"
    for a in tp:
      w2 = a[st]
      if w2 == mns or w2 == pls:
        continue
      else:
        print("Error: Operator must be '+' or '-'.", end="")
        exit()

    st = 0
    for a in tp:
      w1 = a[st]
      if len(w1) < 5:
        continue
      else:
        print("Error: Numbers cannot be more than four digits.", end="")
        exit()

    st = 2
    for a in tp:
      w3 = a[st]
      if len(w3) < 5:
        continue
      else:
        print("Error: Numbers cannot be more than four digits.", end="")
        exit()

    temp = []
    for word in arguments:
      sl = word.split()
      temp.append(sl)

    stl1 = 0
    stl2 = 1
    stl3 = 2
    pov = []
    n = 0
    for b in temp:
      wrd1 = b[stl1]
      wrd2 = b[stl2]
      wrd3 = b[stl3]
      n += 1
      if n == len(temp):
        if len(wrd1) > len(wrd3):
          qi = len(wrd1) - len(wrd3)
          abst = " " * qi
          pov.append(wrd2 + ' ' + abst + wrd3)
        elif len(wrd1) == len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
        elif len(wrd1) < len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
      else:
        if len(wrd1) > len(wrd3):
          qi = len(wrd1) - len(wrd3)
          abst = " " * qi
          pov.append(wrd2 + ' ' + abst + wrd3)
        elif len(wrd1) == len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)
        elif len(wrd1) < len(wrd3):
          pov.append(wrd2 + ' ' + wrd3)

    y = " " * 4
    stl = 0
    n = 0
    for a, los in zip(temp, pov):
      wrd1 = a[stl]
      n += 1
      if n == len(temp):
        if len(los) == 6:
          print(f"{wrd1: >10}", "\n", end="")
        if len(los) == 5:
          print(f"{wrd1: >9}", "\n", end="")
        if len(los) == 4:
          print(f"{wrd1: >8}", "\n", end="")
        if len(los) == 3:
          print(f"{wrd1: >7}", "\n", end="")
      elif n == 1:
        if len(los) == 6:
          print(f"{wrd1: >6}", end="")
        if len(los) == 5:
          print(f"{wrd1: >5}", end="")
        if len(los) == 4:
          print(f"{wrd1: >4}", end="")
        if len(los) == 3:
          print(f"{wrd1: >3}", end="")
      else:
        if len(los) == 6:
          print(f"{wrd1: >10}", end="")
        if len(los) == 5:
          print(f"{wrd1: >9}", end="")
        if len(los) == 4:
          print(f"{wrd1: >8}", end="")
        if len(los) == 3:
          print(f"{wrd1: >7}", end="")

    stl1 = 0
    stl2 = 1
    stl3 = 2
    g = []
    n = 0
    for b in temp:
      wrd1 = b[stl1]
      wrd2 = b[stl2]
      wrd3 = b[stl3]
      n += 1
      if n == len(temp):
        if len(wrd1) > len(wrd3):
          z = len(wrd1) - len(wrd3)
          abst = " " * z
          g.append(wrd2 + ' ' + abst + wrd3)
          print(f"{wrd2 + ' ' + abst + wrd3 + y:>}", "\n", end="")
        elif len(wrd1) == len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", "\n", end="")
        elif len(wrd1) < len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", "\n", end="")
      else:
        if len(wrd1) > len(wrd3):
          z = len(wrd1) - len(wrd3)
          abst = " " * z
          g.append(wrd2 + ' ' + abst + wrd3)
          print(f"{wrd2 + ' ' + abst + wrd3 + y:>}", end="")
        elif len(wrd1) == len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", end="")
        elif len(wrd1) < len(wrd3):
          g.append(wrd2 + ' ' + wrd3)
          print(f"{wrd2 + ' ' + wrd3 + y:>}", end="")

    i = "-"
    n = 0
    for li in g:
      lin = len(li)
      n += 1
      print(f"{i * lin + y:>}", end="")
1 Like

What error message(s) are you seeing in the console?

So I wrote the code in PyCharm and there are no error messages in the console and everthing works fine, but when I try to run the code in replit, an error occurs:

At the beginning, when I started to work on this projekt, I forgot to select “Use run command”, but the test worked at least and the result was 10 failures.

The conclusion is that I don’t know why the code fails the test and what I did wrong in replit.

From the error message, it sounds like pytest was not installed by replit.

From the Shell (not Console), you can run:

pip install pytest

and then be able to click Run to run the tests.

Thank you, the problem with running the tests etc. is now solved, but I still don’t know why I get 10 failures in the test. Can you give me a hint, please?

Your function should be building a string to return instead of using print. After looking at your code, you have a lot lot of duplicate code that you should consolidate to functions to make your code more readable. Also, make sure to use variable names that describe what data they contain. Variable names like g or z do not make the code readable for someone trying to help you debug.

Thank you again for the observation. Now the code returns something, instead of printing it out and I hope that I managed to make it more readable, but the code still fails the tests.

Link: boilerplate-arithmetic-formatter - Replit

One thing I notice is that you are adding an extra 4 space characters to the end of some lines. For example, for the test case of:

arithmetic_arranger(['3801 - 2', '123 + 49'])

your function returns:

'  3801      123\n-    2    +  49    \n------    -----    '

instead of:

'  3801      123\n-    2    +  49\n------    -----'

So I solved the problem with the spaces and therefore 6 tests pass, but unfortunately I still don’t understand why the 4 test with the errors fail, it says that the “SystemExit” and “errors(lst_for_errs)” are errors. Am I supposed to exit the code differently or is “errors(lst_for_errs)” the problem, because it is not added to “result”? Probably something else.
Thank you in Advance and I have learned a lot so far.

The instructions state:

it will return a string that describes an error that is meaningful to the user

This means, the function must return a string to represent the error messages. Throwing an error is not the same thing as a function returning a string.

Ok, now it works. Thank you you for all the help.