Help... Python Arithmetic Formatter

Tell us what’s happening:
always failure on formatting output… can you help me to see what’s wrong with my code

Your code so far

def arithmetic_arranger(problems, statprint=False):
    # check problem list
    first = ''
    second = ''
    sumx = ''
    lines = ''
    # maximal problems is 5
    if len(problems) >= 6:
        return 'Error: Too many problems.'
    # split problem to separate components
    for i in problems:
        a = i.split()
        firsts = a[0]
        seconds = a[2]
        operands = a[1]
        # check the length of the number, max 4 digits
        if (len(firsts) > 4 or len(seconds) > 4):
            return "Error: Numbers cannot be more than four digits."

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

        if (operands == '+' or operands == '-'):
            if operands == "+":
                sums = str(int(firsts) + int(seconds))
            else:
                sums = str(int(firsts) - int(seconds))
            # set length of sum and top, bottom and line values
            length = max(len(firsts), len(seconds)) + 2
            top = str(firsts).rjust(length)
            bottom = operands + str(seconds).rjust(length - 1)
            line = ''
            res = str(sums).rjust(length)
            for s in range(length):
                line += '-'
            # add to the overall string
            first += top + '    '
            second += bottom + '    '
            lines += line + '    '
            sumx += res + '    '
        else:
            return "Error: Operator must be '+' or '-'."
    # strip out spaces to the right of the string
    first.rstrip()
    second.rstrip()
    lines.rstrip()
    if statprint:
        sumx.rstrip()
        arranged_problems = first + '\n' + second + '\n' + lines + '\n' + sumx
    else:
        arranged_problems = first + '\n' + second + '\n' + lines
    return arranged_problems

Here the output

   32      3801      45      123    
+ 698    -    2    + 43    +  49    
-----    ------    ----    -----    
F..F..
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/fcc-arithmetic-arranger/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: '    [23 chars]  123    \n+ 855    -    2    + 43    +  49   [35 chars]    ' != '    [23 chars]  123\n+ 855    -    2    + 43    +  49\n-----[23 chars]----'
-     3      3801      45      123    
?                                 ----
+     3      3801      45      123
- + 855    -    2    + 43    +  49    
?                                 ----
+ + 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-arithmetic-arranger/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 arithemetic problems and a second argument of `True`.')
AssertionError: '   3[23 chars]  123    \n- 698    - 3801    + 43    +  49   [73 chars]    ' != '   3[23 chars]  123\n- 698    - 3801    + 43    +  49\n-----[57 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 arithemetic problems and a second argument of `True`.

----------------------------------------------------------------------
Ran 6 tests in 0.012s

FAILED (failures=2)

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

solve after add some conditional statement on line 38

# add to the overall string
            if i != problems[-1]:
              first += top + '    '
              second += bottom + '    '
              lines += line + '    '
              sumx += res + '    '
            else:
              first += top
              second += bottom
              lines += line
              sumx += res
2 Likes

you have spaces before the new line character.

yours is the 123 \n, the expected one is 123\n

1 Like

I had a same problem. Thanks for your solution

FYI, you can remove rstrip() after applying @santhika’s solution. I’m a beginner so don’t know exactly why rstrip doesn’t do the job, but negative index does, if anyone knows please leave a comment down below. Thanks @santhika again

2 Likes

Please help me resolve errors in below code:

def arithmetic_arranger(problems, option=False):
    if len(problems) > 5:
        return "Error: Too many problems."
    line_1 = line_2 = line_3 = line_4 = ' '
    for i, problem in enumerate(problems):
        n1, operator, n2 = problem.split()
        number_1, number_2 = len(n1), len(n2)
        max_space = max(number_1, number_2)
        if operator not in ['+', '-']:
            return "Error: Operator must be '+' or '-'."
        if not (n1.isdigit()) and n2.isdigit():
            return "Error: Numbers must only contain digits."
        if number_1 > 4 and number_2 > 4:
            return "Error: Numbers cannot be more than four digits."

        result = int(n1) + int(n2) if operator == '+' else int(n1) - int(n2)

        line_1 = line_1 + f"{n1:>2}"
        line_2 = line_2 + operator + f"{n2:>1}"
        line_3 = line_3 + '' + f"{'-':>1}"
        line_4 = line_4 + str(result).rjust(max_space + 2)

        if i < len(problems) - 1:
            line_1 += ' '
            line_2 += ' '
            line_3 += ' '
            line_4 += ' '

        if option:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}, \n{line_4}"
        else:
            return f"arranged_problems: {line_1}, \n{line_2}, \n{line_3}"
        return arranged_problems

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)

Here’s the link to my repl for easier dubugging https://repl.it/@Itsvu81/FCC-Python-Project-10#arithmetic_arranger.py

1 Like

@andrewcooper81 @jeevithajeev

please create your own topic when you ask for help on a challenge, thank you