Hi, I always get these errors but I dont know why.
def arithmetic_arranger(problems, answer=False):
arranged_problems = ""
upper_line = ""
lower_line = ""
dash_line = ""
result_line = ""
#If there are too many problems supplied to the function. The limit is five, anything more will return an Error
if len(problems) >= 6:
return "Error: Too many problems."
for problem in problems:
left_operand, operator, right_operand = problem.split()
# The appropriate operators the function will accept are addition and subtraction. Multiplication and division will return an error
if operator != "+" and operator != "-":
return "Error: Operator must be '+' or '-'."
# Each number (operand) should only contain digits
if not left_operand.isdigit() or not right_operand.isdigit:
return "Error: Numbers must only contain digits."
#Each operand has a max of four digits in width
if len(left_operand) > 4 or len(right_operand) > 4:
return "Error: Numbers cannot be more than four digits."
# Calculates the answer of the problem
if operator == "+":
sum = str(int(left_operand) + int(right_operand))
else:
sum = str(int(left_operand) - int(right_operand))
# Adds the single parts to their place in the string
length = max(len(left_operand), len(right_operand))
upper_line += str(left_operand.rjust(length + 2)) + " "
lower_line += str(operator + " " + right_operand.rjust(length)) + " "
dash_line += str("-" * (length + 2)) + " "
result_line += str(sum).rjust(length + 2) + " "
if answer:
arranged_problems += upper_line + "\n" + lower_line + "\n" + dash_line + "\n" + result_line
else:
arranged_problems += upper_line + "\n" + lower_line + "\n" + dash_line
return arranged_problems
python main.py
32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
F.EF..
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/test_module.py", line 32, in test_only_digits
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
File "/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py", line 31, in arithmetic_arranger
sum = str(int(left_operand) + int(right_operand))
ValueError: invalid literal for int() with base 10: '3g5'
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-1/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/boilerplate-arithmetic-formatter-1/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.007s
FAILED (failures=2, errors=1)
^C
^C