Test module Error

Tell us what’s happening:
I’m trying to finish the [Scientific Computing with Python][Scientific Computing with Python Projects] and my unit test keep get failures. But when i test it manualy it works fine.
And i cant identify what is the diference. Can some on help me?

Your code so far

def arithmetic_arranger(problems, res=False):
num1 =
num2 =
width =
op =
results =

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

for problem in problems:
    problem = problem.split()
    if problem[1] == '*' or problem[1] == '/':
        return print("Error: Operator must be '+' or '-'.")

    try:
        int(problem[0])
        int(problem[2])
    except:
        return print("Error: Number must only contain digits.")

    if len(problem[0]) > 4:
        return print("Error: Numbers cannot be more than four digits.")
    elif len(problem[2]) > 4:
        return print("Error: Numbers cannot be more than four digits.")

    if len(problem[0]) > len(problem[2]):
        width.append(len(problem[0])+2)
    else:
        width.append(len(problem[2])+2)

    num1.append(problem[0])
    num2.append(problem[2])
    op.append(problem[1])

    if res is True:
        if problem[1] == '+':
            results.append(int(problem[0]) + int(problem[2]))
        else:
            results.append(int(problem[0]) - int(problem[2]))

arranged_problems = ""
count = 0

for n in num1:
    dif = width[count] - len(num1[count])
    if count == 0:
        arranged_problems = ' '*dif + n
        count +=1
    else:
        arranged_problems += ' ' * (dif + 4) + n
        count += 1

second_line = []
count = 0

while count < len(op):
    dif = width[count] - len(num2[count]) - 1

    if len(second_line) == 0:
        second_line = op[count] + ' ' * dif
        second_line = second_line + num2[count]
        count += 1
    else:
        second_line = second_line + '    ' + op[count] + ' ' * dif
        second_line = second_line + num2[count]
        count += 1

third_line = []
count = 0

while count < len(num1):
    if len(third_line) == 0:
        third_line = '-' * width[count]
        count += 1
    else:
        third_line = third_line + '    ' + '-' * width[count]
        count += 1

result_line = []
count = 0

for result in results:
    dif = width[count] - len(str(result))
    if count == 0:
        result_line = ' ' * dif + str(result)
        count += 1
    else:
        result_line += ' ' * (dif + 4) + str(result)
        count += 1

if res == True:
    arranged_problems += str(f'\n{second_line}\n{third_line}\n{result_line}')
else:
    arranged_problems += str(f'\n{second_line}\n{third_line}')

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

Challenge: Arithmetic Formatter

Link to the challenge:

What errors exactly are you getting?

 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

.Error: Operator must be ‘+’ or ‘-’.
FError: Number must only contain digits.
F.Error: Numbers cannot be more than four digits.
FError: Too many problems.
F

FAIL: test_incorrect_operator (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/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):
 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

.Error: Operator must be ‘+’ or ‘-’.
FF.FF

FAIL: test_incorrect_operator (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/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/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+ 3g5 [55 chars]----’ != ‘Error: Numbers must only contain digits.’

  • Error: Numbers must only contain digits.- 98 3801 45 123
    • 3g5 - 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.”

 python main.py
32 3801 45 123

  • 698 - 2 + 43 + 49

.Error: Operator must be ‘+’ or ‘-’.
FError: Number must only contain digits.
F.Error: Numbers cannot be more than four digits.
FError: Too many problems.
F

FAIL: test_incorrect_operator (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/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/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_too_many_digits (test_module.UnitTests)

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/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: None != ‘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.”

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

Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/test_module.py”, line 19, in test_too_many_problems
self.assertEqual(actual, expected, ‘Expected calling “arithmetic_arranger()” with more than five problems to return “Error: Too many problems.”’)
AssertionError: None != ‘Error: Too many problems.’ : Expected calling “arithmetic_arranger()” with more than five problems to return “Error: Too many problems.”


Ran 6 tests in 0.002s

FAILED (failures=4)

Function is expected to return only string, not to print something on it own.

Using return print() does two things. It print outs whatever is passed to the print function, but print function returns None. And that’s what later returned, instead of the actual string.

1 Like

tks so much. You are awesome!

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