Tell us what’s happening:
I think the result I return in the function is formatted differently than expected in the tests. I checked the traceback for both tests but I can’t find exactly how to see the errors (I am a bit new to Python). I think it may be something very silly but it has been hours without finding out a solution.
I appreciate each answer in advance!
Your code so far
def arithmetic_arranger(problems, show = False):
# Lenght of list error:
if len(problems) > 5:
return "Error: Too many problems."
else:
# Assign variable to each line
first_line = ""
second_line = ""
dash_line = ""
answer_line = ""
# Iterate through the operation on the list:
for i in problems:
# Split the operation using whitespaces as separators and saving each value
# on a different variable
items = i.split()
operand_1 = items[0]
operand_2 = items[-1]
symbol = items[1]
# Expected max width in each operation:
width = max(len(operand_1), len(operand_2)) + 2
# Digit only error:
if not operand_1.isdigit() or not operand_2.isdigit():
return "Error: Numbers must only contain digits."
# Symbol specific error:
else:
if symbol == "+":
answer = int(operand_1) + int(operand_2)
elif symbol == "-":
answer = int(operand_1) - int(operand_2)
else:
return "Error: Operator must be '+' or '-'."
# Max length of digits error
if len(operand_1) > 4 or len(operand_2) > 4:
return "Error: Numbers cannot be more than four digits."
# Add to each line its value in order, adjusting width to align them to the right
first_line += str(operand_1).rjust(width)
second_line += symbol +str(operand_2).rjust(width - 1)
dash_line += "-" * width
answer_line += str(answer).rjust(width)
# In case of more than one operation, add 4 whitespaces before adding
# the next set of numbers
if len(problems) > 1:
first_line += " "
second_line += " "
dash_line += " "
answer_line += " "
# Write in a new variable all the lines, defined with newlines to match correctly
# If second value of function set to true, it displays the answers
if show == True:
problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line + "\n" + answer_line)
else:
problem_arranged = (first_line + "\n" + second_line + "\n" + dash_line)
return problem_arranged
The traceback of the two tests:
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/SnarlingHumbleWebpage/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/SnarlingHumbleWebpage/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`.
----------------------------------------------------------------------
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: