I am working on the Arithmetic Arranger problem and I don’t know what the errors I am getting actually mean or how to fix them. The core for loop works, but once I put into replit, I get weird errors that I can’t even understand.
import re
def arithmetic_arranger(problems, solve = False):
if len(problems) > 5:
return "Error: Too many problems."
top = ""
bottom = ""
newDash = ""
newSum = ""
for problem in problems:
if re.search("[*]", problem) or re.search("[/]", problem):
return "Error: Operator must be '+' or '-'"
if re.search("^/s[0-9].+-", problem):
return "Error: Numbers must only contain digits"
firstNumber = problem.split(' ')[0]
operator = problem.split(' ')[1]
secondNumber = problem.split(' ')[2]
if len(firstNumber) >= 5 or len(secondNumber) >= 5:
return "Error: Numbers cannot be more than four digits."
sum =""
if(operator == "+"):
sum = str(int(firstNumber)+int(secondNumber))
else:
sum = str(int(firstNumber)-int(secondNumber))
length = max(len(firstNumber), len(secondNumber)) +2
topLine = str(firstNumber).rjust(length)
bottomLine = operator + str(secondNumber).rjust(length-1)
result = sum.rjust(length)
dash = ""
for y in range(length):
dash += "-"
if problem != problems[-1]:
top += topLine + " "
bottom += bottomLine + " "
newDash += dash + " "
newSum += result + " "
else:
top += topLine
bottom += bottomLine
newDash += dash
newSum += result
if solve:
answer = (top + "\n" + bottom + "\n" + newDash + "\n" + newSum)
else:
answer = (top + "\n" + bottom + "\n" + newDash)
return answer
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: