Tell us what’s happening:
All the test in my console run fine. But here when I run the tests they fail.
Anyone have experienced the same issue. I have counted the dashes spaces on the tests and they are the same. It seems it’s a mistake I cannot see.
Your code so far
def arithmetic_arranger(problems, show_answers=True):
line1 = []
line2 = []
line3 = []
line4 = []
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
split_problems = problem.split(" ")
element1 = split_problems[0]
operand = split_problems[1]
element2 = split_problems[2]
if operand != "+" and operand != "-":
return "Error: Operator must be '+' or '-'."
if not element1.isdigit() or not element2.isdigit():
return "Error: Numbers must only contain digits."
if len(element1) > 4 or len(element2) > 4:
return "Error: Numbers cannot be more than four digits."
result = None
space = " "
if operand == "+":
result = int(element1) + int(element2)
if operand == "-":
result = int(element1) - int(element2)
dashes = None
spaces = None
stringResult = str(result)
if len(element1) > len(element2):
length = len(element1) + 2
dashes = ("_" * length)
spaces = (" " * (len(dashes) - len(element1)))
secondSpaces = (" " * (len(dashes) - len(element2) - 1))
lengthResult = len(stringResult)
resultSpaces = " " * (length - lengthResult)
line1.append(spaces + (element1) + space*4)
line2.append(operand + secondSpaces + element2 + space * 4)
line3.append(dashes + space * 4)
line4.append(resultSpaces + stringResult + space * 4)
else:
length = len(element2) + 2
dashes = ("_" * length)
spaces = (" " * (length - len(element1)))
lengthResult = len(stringResult)
resultSpaces = " " * (length - lengthResult)
line1.append( spaces + (element1) + space * 4)
line2.append(operand + space + element2 + space * 4)
line3.append(dashes + space * 4)
line4.append(resultSpaces + stringResult + space * 4)
print(" ".join(line1))
print(" ".join(line2))
print(" ".join(line3))
if show_answers is True:
print(" ".join(line4))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project