Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:

Hi! Basically, my code doesn’t pass any test when the list of problems is valid.
I don’t understand the error message and the output looks ok to me .
error message for the first 2 problems arrangement is :

_______________ test_template[test_two_problems_arrangement1] ________________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert ('  3801      123\n-    2    +  49\n------    -----', True) == '  3801      123\n-    2    +  49\n------    -----'
E         +('  3801      123\n-    2    +  49\n------    -----', True)
E         -'  3801      123\n-    2    +  49\n------    -----'

test_module.py:77: AssertionError
---------------------------- Captured stdout call ----------------------------
  3801      123
-    2    +  49
------    -----

Your code so far

class Problem:
  authorized_operators = ['+', '-']
  authorized_chars = set("1234567890")
  full_list_operators =  set("+-*/")
  
  def __init__(self, description: str):
    self.description = description
    self.splitted_description = description.split()
    self.operator = self.splitted_description[1]
    self.left_operand = self.splitted_description[0]
    self.right_operand = self.splitted_description[2]

  def __str__(self) -> str:
    return f"{self.description}"

  def result(self) -> int:
    return eval(self.description)
  def indented_problem(self):
    column_width = max(len(self.left_operand), len(self.right_operand)) + 2
    indented_left_operand = self.left_operand.rjust(column_width, " ")
    indented_right_operand = self.operator + self.right_operand.rjust(column_width - 1, " ")
    dashed_line = "-" * column_width
    indented_result = str(self.result()).rjust(column_width, " ") 
    return indented_left_operand, indented_right_operand, dashed_line, indented_result

#=========================================
#My function
#=========================================  
def arithmetic_arranger(problems: list, show_solutions: bool = False) -> str:
  if len(problems) > 5:
    return "Error: Too many problems."
  problems_list = []
  for problem in problems:
    problems_list.append(Problem(problem))
  for problem in problems_list:
    if problem.operator not in Problem.authorized_operators:
      return "Error: Operator must be '+' or '-'."
    if not (problem.left_operand.isnumeric() and problem.right_operand.isnumeric()):
      return "Error: Numbers must only contain digits."
    if len(problem.left_operand) > 4 or len(problem.right_operand) > 4:
      return "Error: Numbers cannot be more than four digits."
  arranged_problems = ""
  first_line = ""    #all the left operands
  second_line = ""   #all the operators and right operands
  third_line = ""    #the dashes
  fourth_line = ""   #the solutions
  inter_problem_space = " " *4
  for problem in problems_list:
    first_line += problem.indented_problem()[0] + inter_problem_space
    second_line += problem.indented_problem()[1] + inter_problem_space
    third_line += problem.indented_problem()[2] + inter_problem_space
   
    if show_solutions:
      fourth_line += problem.indented_problem()[3] + inter_problem_space

  arranged_problems = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + third_line.rstrip()
  if show_solutions:
    arranged_problems += "\n" + fourth_line.rstrip()
  print(arranged_problems)
  return arranged_problems, True

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Is your output supposed to have “True” at the end?

1 Like

It looks like the assert output has changed a bit, but:

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

What you print doesn’t really matter. The tests are checking what your function returns

1 Like

Thanks a million for taking out the time to answer. The problem was the “True” I was returning as you pointed out !