Python Arithematic Formatter(Can anyone help me whats wrong in this code?)

import re

def arithmetic_arranger(problems, solve :False):
  
  if solve:
  
    firstLine = ''
    secondLine = ''
    sumP = ''
    #
    if len(problems) > 5:
      return 'Error: Too many problems.'
    
    for problem in problems:
      l = (problem.split(' '))
      firstLine = l[0]
      op = l[1]
      secondLine = l[2]
     
      if len(firstLine) >= 5 or len(secondLine) >= 5 :
        return 'Error: Numbers cannot be more than four digits.'
      if not firstLine.isnumeric() or not secondLine.isnumeric():
        return 'Error: Numbers must only contain digits.'
      
        length = max(len(firstLine), len(secondLine)) + 2
        top = str(firstLine).rjust(length)
        bottom = op + str(secondLine).rjust(length - 1)
        sum = ''
        for s in range(length):
          sum += '-'      
        firstLine += top + '    '
        secondLine += bottom + '    '
        sumP += sum + '    '   
      else: 
        return "Error: Operator must be '+' or '-'.     
      
    firstLine.rstrip()
    secondLine.rstrip()
    sumP.rstrip()

    arranged_problems = firstLine + '\n' + secondLine + '\n' + sumP
  return arranged_problems

welcome!
what issues are you having?
what’s going wrong?

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

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