Tell us what’s happening:
Describe your issue in detail here.
Can someone please help me find the error in this code as it is failing the test
Your code so far
import re
def arithmetic_arranger(problems, ShowAns = False):
if(len(problems) > 5):
return "Error: Too many problems."
FirstLine = ""
SecondLine = ""
Lines = ""
Solution = ""
for problem in problems:
if(re.search("[^\s0-9,+-]", problem)):
if(re.search("[/]"), problem or re.search("[*]"), problem):
return "Error: Operator must be '+' or '-'."
return "Error: Number must only contain digits."
Input = problem.split()
Num1 = Input[0]
Operator = Input[1]
Num2 = Input[2]
if(len(Num1) >= 5 or len(Num2) >= 5):
return "Error: Number cannot be more than four digits."
Ans = " "
if(Operator == '+'):
Ans = str(int(Num1) + int(Num2))
elif(Operator == '-'):
Ans = str(int(Num1) - int(Num2))
length = max(len(Num1), len(Num2)) + 2
top = str(Num1).rjust(length)
bottom = str(Operator) + str(Num2).rejust(length - 1)
line = ""
res = str(Ans).rjust(length)
for s in range(length):
line = "-"
if(problem != problems[-1]):
FirstLine += top + " "
SecondLine += bottom + " "
Lines += line + " "
Solution += res + " "
else:
FirstLine += top
SecondLine += bottom
Lines += line
Solution += res
if(ShowAns == True):
arranged_problems = f"{FirstLine}\n{SecondLine}\n{Lines}\n{Solution}"
else:
arranged_problems = f"{FirstLine}\n{SecondLine}\n{Lines}"
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: