Arithmetic Arranger Python Certification Project

Will someone please tell me what I’m doing wrong?

def arithmetic_arranger(problems,show=False):
    line1="" 
    line2="" 
    line3="" 
    line4="" 
    op="" 
    answer=""
    sumline="" 


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

    for p in problems:
        num1=p.split(" ")[0]
        op=p.split(" ")[1]
        num2=p.split(" ")[2]

    if (len(num1)>4 or len(num2)>4):
        return "Error: Numbers cannot be more than four digits."
    if not num1.isdigit() or not num2.isdigit():
        return "Error: Numbers must only contain digits."
    if (op=="+" or op=="-"):
        if op=="+":
            solution=str(int(num1)+int(num2))
        if op=="-":
            solution=str(int(num1)-int(num2))
    else:
        return "Error: Operator must be '+' or '-'."

    length=max(len(num1),len(num2))+2
    firstline=str(num1).rjust(length)
    secondline=op+str(num2.rjust(length-1))
    sumline=str(solution.rjust(length))

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

    if p!=problems[-1]:
        line1+=firstline+"    "
        line2+=secondline+"    "
        line3+=dash+"    "
        line4+=sumline+"    "
    else:
        line1+=firstline
        line2+=secondline
        line3+=dash
        line4+=sumline

    if show:
        answer=line1+'\n'+line2+'\n'+line3+'\n'+line4
    else:
        answer=line1+'\n'+line2+'\n'+line3
    return answer

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 (’).

what do the tests say?

 python main.py
  123
+  49
-----
None
  123
+  49
-----
F  123
+  49
-----
F  123
+  49
-----
F  123
+  49
-----
  172
F  123
+  49
-----
F.
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/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: None != '    3      3801      45      123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]

======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 24, in test_incorrect_operator
    self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: None != "Error: Operator must be '+' or '-'." : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/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: None != 'Error: Numbers must only contain digits.' : 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-2/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`.')
 python main.py
  123
+  49
-----
FFFFF.
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/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: '  123\n+  49\n-----' != '    3      3801      45      123\n+ 855    [53 chars]----'
-   123
- +  49
- -----+     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_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/test_module.py", line 24, in test_incorrect_operator
    self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: '  123\n+  49\n-----' != "Error: Operator must be '+' or '-'."
+ Error: Operator must be '+' or '-'.-   123
- +  49
- ----- : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."

======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/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: '  123\n+  49\n-----' != 'Error: Numbers must only contain digits.'
+ Error: Numbers must only contain digits.-   123
- +  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-2/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: '  123\n+  49\n-----\n  172' != '   32         1      45      123\n- 698    [87 chars] 172'
-   123
- +  49
- -----
-   172+    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`.

======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-arithmetic-formatter-2/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: '  123\n+  49\n-----' != 'Error: Numbers cannot be more than four digits.'
+ Error: Numbers cannot be more than four digits.-   123
- +  49
- ----- : 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.004s

FAILED (failures=5)

FIGURED IT OUT!!!

I got the indentation wrong starting at line 19 up until line 51

line 19:

if (len(num1)>4 or len(num2)>4):

up until line 51:

if show:

all code in lines 19-51 (19 included, 51 not) need to be indented once more each

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