Arithmetic Formatter

So, I am stuck on this problem. My output is identical, but I am not passing the three tests.
Below is my code,

def check_operators(element):
  expression = element[1]
  if expression not in ['+', "-"]:

    return "Error: Operator must be '+' or '-'."


def check_digit(element):
  if not (element[0].isdigit() and element[2].isdigit()):
    return "Error: Numbers must only contain digits."
  if (len(element[0]) > 4 or len(element[2]) > 4):
    return "Error: Numbers cannot be more than four digits."


def arithmetic_arranger(problems, show_result=False):
  arranged_arr = []
  error_messages = []
  if (len(problems) > 5):
    return "Error: Too many problems."

  for problem in problems:
    operator_error = check_operators(problem.split())
    digit_error = check_digit(problem.split())

    if operator_error:
      error_messages.append(operator_error)
    elif digit_error:
      error_messages.append(digit_error)
    else:
      arranged_arr.append(arrange(problem.split(), show_result))
  formatted_lines = []

  for line_group in zip(*arranged_arr):
    joined_group = "    ".join(line_group)
    formatted_lines.append(joined_group)

  final_formatted_output = "\n".join(formatted_lines)
  return final_formatted_output


def arrange(element, show_result=False):
  first_elem = element[0]
  expression = element[1]
  second_elem = element[2]
  if expression not in ['+', "-"]:
    return "Error: Operator must be '+' or '-'."
  number_of_dashes = max(len(first_elem), len(second_elem))
  first_line = ""
  second_line = ""
  dashes_line = ""
  if show_result:
    result = eval(' '.join(element))
  else:
    result = ""
  width = number_of_dashes + 2
  whitespace_without_operator = " " * (width - len(first_elem))
  first_line += f"{whitespace_without_operator}{first_elem}"
  whitespace_with_operator = " " * \
      (len(first_line) - len(f"{expression}{second_elem}"))
  second_line += f"{expression}{whitespace_with_operator}{second_elem}"
  dashes_line += f"{'-' * width}"
  if show_result:
    whitespace_for_result = " " * (width - len(str(result)))
    third_line = f"{whitespace_for_result}{result}"
    return first_line, second_line, dashes_line, third_line
  return first_line, second_line, dashes_line

the three tests that I am not passing are,


I checked in my local machine and it is passing the tests but not when I click the run button in replit. Any suggestion is appreciated.

If you hit an error, you need to immediately stop and return.

You’re instead ignoring the error and skipping the offending problems

def check_operators(element):
  expression = element[1]
  if expression not in ['+', "-"]:
    return "Error: Operator must be '+' or '-'."


def check_digit(element):
  if not (element[0].isdigit() and element[2].isdigit()):
    return "Error: Numbers must only contain digits."
  if (len(element[0]) > 4 or len(element[2]) > 4):
    return "Error: Numbers cannot be more than four digits."


def arithmetic_arranger(problems, show_result=False):
  arranged_arr = []
  if (len(problems) > 5):
    return "Error: Too many problems."

  for problem in problems:
    check_operators(problem.split())
    check_digit(problem.split())
    arranged_arr.append(arrange(problem.split(), show_result))
  formatted_lines = []

  for line_group in zip(*arranged_arr):
    joined_group = "    ".join(line_group)
    formatted_lines.append(joined_group)

  final_formatted_output = "\n".join(formatted_lines)
  return final_formatted_output


def arrange(element, show_result=False):
  first_elem = element[0]
  expression = element[1]
  second_elem = element[2]
  if expression not in ['+', "-"]:
    return "Error: Operator must be '+' or '-'."
  number_of_dashes = max(len(first_elem), len(second_elem))
  first_line = ""
  second_line = ""
  dashes_line = ""
  if show_result:
    result = eval(' '.join(element))
  else:
    result = ""
  width = number_of_dashes + 2
  whitespace_without_operator = " " * (width - len(first_elem))
  first_line += f"{whitespace_without_operator}{first_elem}"
  whitespace_with_operator = " " * \
      (len(first_line) - len(f"{expression}{second_elem}"))
  second_line += f"{expression}{whitespace_with_operator}{second_elem}"
  dashes_line += f"{'-' * width}"
  if show_result:
    whitespace_for_result = " " * (width - len(str(result)))
    third_line = f"{whitespace_for_result}{result}"
    return first_line, second_line, dashes_line, third_line
  return first_line, second_line, dashes_line


# print(arithmetic_arranger(['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']))

I refactored to this, shouldn’t this work?

These errors still don’t immediately stop the function.

Run zero additional code once you find a single error.

why is this happening? I have used return inside the functions. shouldn’t the function return the string and stop the function?

Returning from those functions doesn’t make the function that called them immediately stop any further execution. Otherwise it would be impossible to compose function calls.

thanks. that helped a lot. Now passed all the tests

1 Like

If that’s now it worked, then this function call would also require your arithmetic_arranger to immediately stop and execute no further code.

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