Tell us what’s happening:
Hi,
I finished the Arithmetic Formatter test but somehow I don’t understand why it’s not passing the test ? The output is identical to what’s asked …
Your code so far
def arithmetic_arranger(problems, show_solutions=False):
line1 = ""
line2 = ""
line3 = ""
line4 = ""
if len(problems) > 5:
return "Error: Too many problems."
operations = {"+": lambda a, b: a + b, "-": lambda a, b: a - b}
for problem in problems:
parts = problem.split(" ")
if parts[1] not in operations:
return "Error: Operator must be '+' or '-'."
if not parts[0].isnumeric() or not parts[2].isnumeric():
return "Error: Numbers must only contain digits."
if len(parts[0]) > 4 or len(parts[2]) > 4:
return "Error: Numbers cannot be more than four digits."
num1 = parts[0]
operator = parts[1]
num2 = parts[2]
hyphen = "-" * (max(len(num1), len(num2)) + 2)
line1 += "{:>6}".format(num1) + " "
if len(num1) >= len(num2):
line2 += "{:>6}".format(operator + " " * (len(num1) - len(num2) + 1) + num2) + " "
else:
line2 += "{:>6}".format(operator + " " + num2) + " "
line4 += "{:>6}".format(operations[operator](int(num1), int(num2))) + " "
line3 += "{:>6}".format(hyphen) + " "
lines = [line1, line2, line3]
if show_solutions:
lines.append(line4)
return "\n".join(line.rstrip() for line in lines)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: