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

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.

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?

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

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.

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.