Python challenge 1: Arithmetic Arranger

Can someone help me check where my code went wrong please?
I keep getting failures on test_solutions and test_arrangement

def arithmetic_arranger(problems, show = False):
  firstAnswer = ""
  operators = ""
  dashlines = ""
  secondAnswer = ""
  sumup = ""
  answer = ""


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

  for problem in problems:
    #spliting the strings into a list
    firstNumber = problem.split(" ")[0]
    operators = problem.split(" ")[1]
    secondNumber = problem.split(" ")[2]

    # check the length of the number, max 4 digits
    if (len(firstNumber) > 4 or len(secondNumber) > 4):
      return "Error: Numbers cannot be more than four digits."

    # check the input as valid digits
    if not firstNumber.isnumeric() or not secondNumber.isnumeric():
      return "Error: Numbers must only contain digits."

    # check for the correct form of operators
    if operators != "+" or "-":
      return "Error: Operator must be '+' or '-'."

    sum = ""
    if operators == "+": 
      sum = str(int(firstNumber) + int(secondNumber))
    elif operators == "-":
      sum = str(int(firstNumber) - int(secondNumber))

    length = max(len(firstNumber) , len(secondNumber)) + 2
    fisrtline = str(firstNumber).rjust(length)
    secondline = operators + str(secondNumber).rjust(length - 1) 
    sltn = str(sum.rjust(length))

    dashline = ""
    for dash in range(length):
      dashline += "-"

    if problem != problems[-1]: 
      firstAnswer += fisrtline + "  "
      secondAnswer += secondline + "  "
      dashlines += dashline + "  "
      sumup += sltn + "  "
    else: 
      firstAnswer += fisrtline
      secondAnswer += secondline 
      dashlines += dashline 
      sumup += sltn 



  if show: 
    answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines + "\n" + sumup
  else:
    answer = firstAnswer + "\n" + secondAnswer + "\n" + dashlines    
  return answer

Here is the output:

 python main.py
Error: Operator must be '+' or '-'.
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FCC-Python-Project-10/test_module.py", line 10, in test_arrangement
    self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: "Error: Operator must be '+' or '-'." != '    3      3801      45      123\n+ 855 [56 chars]----'
- Error: Operator must be '+' or '-'.
+     3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
 : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/FCC-Python-Project-10/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: "Error: Operator must be '+' or '-'." != '   32         1      45      123\n- 698 [90 chars] 172'
- Error: Operator must be '+' or '-'.
+    32         1      45      123
- 698    - 3801    + 43    +  49
-----    ------    ----    -----
 -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`.

----------------------------------------------------------------------
Ran 6 tests in 0.003s

FAILED (failures=2)

Notice that test indicates function returns Error: Operator must be '+' or '-'. for a case where returned should be arranged problems.

Take a closer look at the way function checks if problem is using allowed operator.

I think the error lies in Test Solution and Test Arrangement (the first and last section in the test_module.py file) because there are two long paragraphs of traceback on that. And the last line in the traceback says there are only 2 failures.

I also don’t know why there’s a “+” in front of 32 on the first line

the + there signal the expected output

you are returning

instead of the arranged numbers

How can I fix that? I’m a beginner and so far i don’t see the difference between

and other Error Return Codes that I wrote
But thank you so much for pointing that out

It’s specifically that part of code, as there isn’t any other way for function to return "Error: Operator must be '+' or '-'.". For some reason it’s returned when it shouldn’t. Try to figure out why that’s happening.

just above here I would try print(operators != '+' or '-')
and see what is printed

i fixed this part, now it’s returning

   32      3801      45      123
+ 698   -    2   + 43   +  49
-----   ------   ----   -----

does that passes the tests?

nope cos the first line items are sticking out from 1 space, to 2 spaces, to 3 spaces

my code

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