Tell us what’s happening:
Describe your issue in detail here.
Hi everyone, so I’ve been trying to finish the arithmetic formatter and I’ve reached the end of my code. However, I have 1 failure with 5 errors and I can’t seem to find where the errors are. Also as you can see the output for the first list given is not fully printed as I have probably done the tabulation for arranged_problems where it is not suppose to be done.
Thanks a lot in advance.
Your code so far
def arithmetic_arranger(problems, see):
""" Declaration of list, variables and strings used in program """
first_operand = list()
operator = list()
second_operand = list()
max_length = list()
dashes = list()
operation = list()
upper_part = ""
lower_part = ""
dashesline = ""
results = ""
index = 0
# Checking if the length of the list is above 5
if len(problems) > 5:
print(" Error: Too many problems")
# Divide the list into sub lists
for increment in problems:
new_item = increment.split()
# Divide the list into sub list
first_operand.append(new_item[0])
operator.append(new_item[1])
second_operand.append(new_item[2])
""" Checking if the operands are only digits (must be separated into two different for loops if we do not want to see the error written 4 times) """
for go_over in first_operand:
if not go_over.isnumeric():
print("Error: Numbers must only contain digits")
for go_over in second_operand:
if not go_over.isnumeric():
print("Error: Numbers must only contain digits")
""" Checking if numbers are not above 4 digits (must be separated into two different for loops if we do not want to see the error written 4 times) """
for go_over in first_operand:
if len(go_over) > 4:
print("Error: Numbers cannot be more than four digits")
for go_over in second_operand:
if len(go_over) > 4:
print("Error: Numbers cannot be more than four digits")
# Checking if the operator is a '+' or a '-'
for go_over in operator:
index += 1
if go_over == "*" or go_over == "/":
print("Error: Operator must be '+' or '-'")
# Doing the calculation for '+' and '-'
elif go_over == "+":
operation.append(str(int(first_operand[index-1]) + int(second_operand[index-1])))
elif go_over == "-":
operation.append(str(int(first_operand[index-1]) - int(second_operand[index-1])))
""" Having the maximum length of each calculation # and the numbers of dashes needed for each of those calculations """
for go_over in range(len(first_operand)):
max_length.append(max(len(first_operand[go_over]),len(second_operand[go_over])))
dashes.append("-"*(2+max_length[go_over]))
for go_over in range(len(first_operand)):
if see == True:
if go_over == 0:
upper_part += first_operand[go_over].rjust(2+max_length[go_over])
lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
dashesline += dashes[go_over]
results += operation[go_over].rjust(2+max_length[go_over])
else:
upper_part += " " + first_operand[go_over].rjust(2+max_length[go_over])
lower_part += " " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])
dashesline += " " + dashes[go_over]
results += " " + operation[go_over].rjust(2+max_length[go_over])
arranged_problems = upper_part + "\n" + lower_part + "\n" + dashesline + "\n" + results
else:
if go_over == 0:
upper_part += first_operand[go_over].rjust(2+max_length[go_over])
lower_part += operator[go_over].ljust(0) + " " + second_operand[go_over]
else:
upper_part += " " + first_operand[go_over].rjust(2+max_length[go_over])
lower_part += " " + operator[go_over].ljust(0) + " " + second_operand[go_over].rjust(max_length[go_over])
arranged_problems = upper_part + "\n" + lower_part
return arranged_problems
Here is the response that I get from the console
python main.py
32
+ 698
-----
730
EEEFEE
======================================================================
ERROR: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 8, in test_arrangement
actual = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'
======================================================================
ERROR: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 22, in test_incorrect_operator
actual = arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'
======================================================================
ERROR: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 32, in test_only_digits
actual = arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'
======================================================================
ERROR: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 27, in test_too_many_digits
actual = arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'
======================================================================
ERROR: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/test_module.py", line 17, in test_too_many_problems
actual = arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])
TypeError: arithmetic_arranger() missing 1 required positional argument: 'see'
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-13/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: ' 32\n- 698\n-----\n -666' != ' 32 1 45 123\n- 698 - 3[84 chars] 172'
- 32
- - 698
- -----
- -666+ 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`.
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=1, errors=5)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: