Arithmetic Formatter: I think my code works but it doesn't pass

Tell us what’s happening:
Describe your issue in detail here.
I think I have finished the arithmetic formatter and it looks like the code output is correct, but when it is put in the tester it only passes 2 tests. It looks like it follows all the rules for the challenge.
The error:
 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

F.FFF.

FAIL: test_arrangement (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 14, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]’)
AssertionError: ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 -938[38 chars]----’ != ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 - 9[41 chars]----’

  • 11 3801 1 123 1
  • 11 3801 1 123 1
    ? +
    • 4 - 2999 + 2 + 49 -9380
    • 4 - 2999 + 2 + 49 - 9380
      ? +
  • ---- ------ — ----- -----+ ---- ------ — ----- ------? +
    : Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]

======================================================================
FAIL: test_only_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 34, in test_only_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’)
 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

F.FFF.

FAIL: test_arrangement (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 14, in test_arrangement
self.assertEqual(actual, expected, ‘Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]’)
AssertionError: ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 -938[38 chars]----’ != ’ 11[29 chars] 1\n+ 4 - 2999 + 2 + 49 - 9[41 chars]----’

  • 11 3801 1 123 1
  • 11 3801 1 123 1
    ? +
    • 4 - 2999 + 2 + 49 -9380
    • 4 - 2999 + 2 + 49 - 9380
      ? +
  • ---- ------ — ----- -----+ ---- ------ — ----- ------? +
    : Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]

======================================================================
FAIL: test_only_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 34, in test_only_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’)
AssertionError: ’ 98 3801 45 123\n+ 3 - [49 chars]----’ != ‘Error: Numbers must only contain digits.’

  • Error: Numbers must only contain digits.- 98 3801 45 123
    • 3 - 2 + 43 + 49
  • — ------ ---- ----- : Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”

======================================================================
FAIL: test_solutions (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 39, in test_solutions
self.assertEqual(actual, expected, ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.’)
AssertionError: ’ 32 1 45 123\n-698 -38[79 chars] 172’ != ’ 32 1 45 123\n- 698 [87 chars] 172’

  • 32 1 45 123
  • 32 1 45 123
    ? + +
  • -698 -3801 + 43 + 49
    • 698 - 3801 + 43 + 49
      ? + +


  • -666 -3800 88 172+ -666 -3800 88 172? + +
    : Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-3/test_module.py”, line 29, in test_too_many_digits
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with a problem that has a number over 4 digits long to return “Error: Numbers cannot be more than four digits.”’)
AssertionError: ‘Error: Numbers cannot be more than four digits’ != ‘Error: Numbers cannot be more than four digits.’

  • Error: Numbers cannot be more than four digits
  • Error: Numbers cannot be more than four digits.
    ? +
    : Expected calling “arithmetic_arranger()” with a problem that has a number over 4 digits long to return “Error: Numbers cannot be more than four digits.”

Ran 6 tests in 0.029s

FAILED (failures=4)

**Your code so far**
import re

def arithmetic_arranger(problems, show = False):
  first_line = ""
  second_line = ""
  sum_dashes = ""
  answer_line = ""

  if len(problems) > 5:
    return "Error: Too many problems."

  for problem in problems:
    problem_split = re.findall(r"[\d]+", problem)
    number = []

    if "*" in problem or "/" in problem:
      return "Error: Operator must be '+' or '-'."

    for item in problem_split:
      try:
        number.append(int(item))
        if len(item) > 4:
          return "Error: Numbers cannot be more than four digits"
      except:
        return "Error: Numbers must only contain digits"
    if "+" in problem:
      operator = "+"
      answer = number[0] + number[1]
    if "-" in problem:
      operator = "-"
      answer = number[0] - number[1]

    ans_long_num = [answer, number[0], number[1]]
    longest_value_len = None
    for value in ans_long_num:
      if longest_value_len == None:
        longest_value_len = len(str(value))
      elif len(str(value)) > longest_value_len:
        longest_value_len = len(str(value))

    if longest_value_len == len(str(number[1])) or longest_value_len == len(str(number[0])):
      longest_value_len = longest_value_len + 2

    sum_dashes = sum_dashes + ("-"  * longest_value_len + "    ")
    first_line = first_line + (str(number[0]).rjust(longest_value_len, " ") + "    ")
    second_line = second_line + operator + (str(number[1]).rjust(longest_value_len - 1, " ") + "    ")
    answer_line = answer_line + (str(answer).rjust(longest_value_len, " ") + "    ")

  if show == True:
    arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + sum_dashes.rstrip() + "\n" + answer_line.rstrip()
  if show == False:
    arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + sum_dashes.rstrip()

  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/90.0.4430.212 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

To help us help you, please provide the error messages from the test suite.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

first F:

When I run your code, I see the error in the result. You have one missing space in the last subtraction column:
img_first-f
The numbers are correct and overall it looks functional, but the format is a tiny bit off.

If I may recommend, I believe if you have a look at string .format method, it’ll make this exercise much easier for you.
I’ll try and have a look at the other Fs, but I hope this helps. :slight_smile:

second F:

I this Error the message says you should NOT accept any input that contains anything else than digits. When running your code with one of the formulas having a letter in it, your code discards the letter and the rest of digits after it, instead of an error message. e.g. passing ‘98+3g5’ as the first formula looks like this:

img_second-f

You can have a look in the test file to look up the line number of where the error hapens (in this case it’s line 34 of the test file) and try a similar case on your device, to see what your code does.

The same goes for the third F - just spacing error:

img_third-f

last F:

The error is in the missing dot. :slight_smile: I make these all the time, too… They are a pain to catch, if they get overlooked the first time around.
img_fourth-f

Thank you for all the replies, I’ll try and fix all the errors.

btw I fixed it all now

Well done! Congratulations :slight_smile:
Good luck in the next challenges!

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