# Arithmetic Formatter: What is wrong with my code?

Tell us what’s happening:
Describe your issue in detail here.

Hi everyone.
I don’t really know what is wrong with my code. It seems to work locally however, I am failing all the tests. Could someone help me to identify why I am failing the tests. Below is my code

**

def arithmetic_arranger(problems, show = False):
""" This function  receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side.
parameters
problems : a list of strings that are arithmetic problems

"""
# variable names
first_line = ""
second_line = ""
dash_line = ""
result_line = ""

# Situations that will return an error
if len(problems) > 5:
return print("Error: Too many problems.")

for string in problems:
operand1 = string.split()[0]
operand2 = string.split()[2]
operator = string.split()[1]

if (operator != "-") & (operator != "+"):
return print("Error: Operator must be '+' or '-'.")

if (not operand1.isdigit()) | (not operand2.isdigit()):
return print("Error: Numbers must only contain digits.")

if (len(operand1) > 4) | (len(operand2)) > 4:
return print("Error: Numbers cannot be more than four digits.")

dash_len = max(len(operand1), len(operand2)) + 2
result = eval(string)

# Conversion in case of correct format supplied by the user
first_line = first_line + " " * (dash_len - len(operand1)) + operand1 + " " * 4
second_line = second_line + operator + " " * (dash_len - 1 - len(operand2)) + operand2 + " " * 4
dash_line = dash_line + "-" * dash_len  + " " * 4
result_line = result_line + " " * (dash_len - len(str(result))) + str(result) + " " * 4

first_line = first_line[:-4]        # This is the line of upper operands including spaces
second_line = second_line[:-4]      # This is the line of lower operands, including the operator and spaces
dash_line = dash_line[:-4]          # This is the dashed line below the first two lines. It also includes spaces
result_line = result_line[:-4]      # This is the result line to display the evaluation of operations

vert_ops = first_line + '\n' + second_line + '\n' + dash_line

if show:
arranged_problems = print(vert_ops + '\n' + result_line)
return arranged_problems
else:
arranged_problems = print(vert_ops)
return arranged_problems

**

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

Challenge: Arithmetic Formatter

are you sure that returning the output of a print statement is what you want to do?

2 Likes

If you got such a question, please provide at least the actual error messages, maybe even a link to your project

Here is the link to my project
https://replit.com/@JoeArmel/boilerplate-arithmetic-formatter#arithmetic_arranger.py

Below are the error messages

 python main.py
32      3801      45      123
+ 698    -    2    + 43    +  49
-----    ------    ----    -----
None
3      3801      45      123
+ 855    -    2    + 43    +  49
-----    ------    ----    -----
FError: Operator must be '+' or '-'.
FError: Numbers must only contain digits.
F   32         1      45      123
- 698    - 3801    + 43    +  49
-----    ------    ----    -----
-666     -3800      88      172
FError: Numbers cannot be more than four digits.
FError: Too many problems.
F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/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/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_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/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: None != '   32         1      45      123\n- 698 [90 chars] 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/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.036s

FAILED (failures=6)

Thanks for your remark. I am done

1 Like

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

Take some time and actually read the error message, they are actually quite helpful now and later on
Most importantly the AssertionError shows first your output and then the expected output. So they told you that all your return values were None instead of text-strings.

You are right. I have fixed the problems. I was outputing print statements instead of strings values.