Need Help With Arithmetic Formatter

Not quite sure if I’m reading the console wrong or what but there seems to be a rogue “+” and space disrupting the top line of the printed code and I’m not sure as to what’s causing it. With that being said any help would be much appreciated.

My Code:

def arithmetic_arranger(problems, answer=False):

  import operator

  ops = {"+" : operator.add, "-" : operator.sub}

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

  for problem in problems:
    splt = problem.split()

    if not splt[0].isdigit() or not splt[2].isdigit():
      return "Error: Numbers must only contain digits."

    if splt[1] != '+' or '-':
      return "Error: Operator must be '+' or '-'."

    if len(splt[0]) or len(splt[2]) > 4:
      return "Error: Numbers cannot be more than four digits."

    largest_num = max(splt, key=len)
      
    if len(splt[0]) == len(splt[2]):
      bottom = splt[1] + " " + splt[2] 
      
    elif splt[2] == largest_num:
      bottom = splt[1] + " " + splt[2]
      
    else: bottom = splt[1] + (" "*(len(largest_num)-len(splt[2]))) + splt[2]

    top = (" "*(len(bottom) - len(splt[0]))) + splt[0]
    print(top)
      
    line = ('-'*len(max(bottom, top, key = len)))
      
    arranged_problems = top + '\n' + bottom + '\n' + line
      
    summ = 0

    if answer == True:
      summ = str(ops["{}".format(splt[1])](top, bottom))
      arranged_problems = top + '\n' + bottom + '\n' + line + '\n' + summ

      print(arranged_problems)

  return arranged_problems

Not sure as to how to copy paste the console.

If you write it on Replit, there should be an option to share it. This way we could just run the code and see what happens.
Because without it, giving feedback is pretty hard.

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