I have designed my arithmetic arranger. I have supplied print calls so you can easily see that things look the same. There are some errors in the browser console but I don’t understand what it means. I will provide the code as well as the errors it is producing in the browser console. In my python terminal everything seems to match what is expected.
MY CODE:
def check_problem_length(problems):
if len(problems) > 5:
return True
else:
return False
def check_operand_validity(uppers,lowers):
#You need to create a function to set an error for Error: Numbers must only contain digits.
numbers = uppers + lowers
for number in numbers:
if not number.isdigit():
return True
def check_operators_validity(operators):
for operator in operators:
if operator == "+":
continue
elif operator == "-":
continue
else:
return True
def check_numeral_lengths(uppers,lowers):
for index in range(len(uppers)):
if len(str(uppers[index])) > 4 or len(str(lowers[index])) > 4:
return True
def problem_lists(problems):
uppers = []
operators = []
lowers = []
for string in problems:
split_string = string.split(" ")
uppers.append(split_string[0])
operators.append(split_string[1])
lowers.append(split_string[2])
return [uppers,operators,lowers]
def check_for_errors(problems, uppers, operators, lowers):
error = ""
if check_problem_length(problems):
error = "Error: Too many problems."
if check_operand_validity(uppers,lowers):
error = "Error: Numbers must only contain digits."
if check_operators_validity(operators):
error = "Error: Operator must be '+' or '-'."
if check_numeral_lengths(uppers,lowers):
error = "Error: Numbers cannot be more than four digits."
return error
def _s(number_of_spaces):
return " " * number_of_spaces
def calculate_answer(upper,operator,lower):
try:
if operator == "+":
return int(upper) + int(lower)
elif operator == "-":
return int(upper) - int(lower)
except ValueError as typer:
pass
def arithmetic_arranger(problems, show_answers=False):
uppers,operators,lowers = problem_lists(problems)
if check_for_errors(problems, uppers, operators, lowers):
return check_for_errors(problems, uppers, operators, lowers)
upper_digit_length = 0
lower_digit_length = 0
digit__dif = 0
first_string = ""
second_string = ""
third_string = ""
fourth_string = ""
for index in range(len(uppers)):
answer = str(calculate_answer(uppers[index],operators[index],lowers[index]))
upper_digit_length = len(uppers[index])
lower_digit_length = len(lowers[index])
digit_dif = upper_digit_length - lower_digit_length
longest_number_length = max(len(uppers[index]),len(lowers[index]))
total_length = longest_number_length + 2
if digit_dif > 0: #its positive, added space on bottom
first_string += _s(2) + uppers[index] + _s(4)
second_string += operators[index] + _s(1 + abs(digit_dif)) + lowers[index] + _s(4)
third_string += "-" * total_length + _s(4)
fourth_string += _s(total_length - len(answer)) + answer + _s(4)
elif digit_dif < 0: #its negative, added space on top
first_string += _s(abs(digit_dif) + 2) + uppers[index] + _s(4)
second_string += operators[index] + _s(1) + lowers[index] + _s(4)
third_string += "-" * total_length + _s(4)
fourth_string += _s(total_length - len(answer)) + answer + _s(4)
else:
first_string += _s(2) + uppers[index] + _s(4)
second_string += operators[index] + _s(1) + lowers[index] + _s(4)
third_string += "-" * total_length + _s(4)
fourth_string += _s(total_length - len(answer)) + answer + _s(4)
if show_answers:
return first_string + "\n" + second_string + "\n" + third_string + "\n" + fourth_string
return first_string + "\n" + second_string + "\n" + third_string
my_answer_1 = arithmetic_arranger(["3801 - 2", "123 + 49"])
print("My answer 1:")
print(my_answer_1)
print("Their answer 1:")
print(" 3801 123\n- 2 + 49\n------ -----")
my_answer_2 = arithmetic_arranger(["1 + 2", "1 - 9380"])
print("My answer 2:")
print(my_answer_2)
print("Their answer 2:")
print(" 1 1\n+ 2 - 9380\n--- ------")
my_answer_3 = arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])
print("My answer 3:")
print(my_answer_3)
print("Their answer 3:")
print(" 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----")
my_answer_4 = arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])
print("My answer 4:")
print(my_answer_4)
print("Their answer 4:")
print(" 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------")
#All of the error handling passes
my_answer_9 = arithmetic_arranger(["3 + 855", "988 + 40"], True)
print("My answer 9:")
print(my_answer_9)
print("Their answer 9:")
print(" 3 988\n+ 855 + 40\n----- -----\n 858 1028")
my_answer_10 = arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
print("My answer 10:")
print(my_answer_10)
print("Their answer 10:")
print(" 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028")
CONSOLE ERRORS:
ERROR - 1:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
AssertionError: ' 3801 123 \n- 2 + 49 \n------ ----- ' != ' 3801 123\n- 2 + 49\n------ -----'
- 3801 123
? ----
+ 3801 123
- - 2 + 49
? ----
+ - 2 + 49
- ------ ----- ? ----
+ ------ -----
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module._pythonexc2js (pyodide.asm.js:9:654797)
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
ERROR - 2:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
AssertionError: ' 1 1 \n+ 2 - 9380 \n--- ------ ' != ' 1 1\n+ 2 - 9380\n--- ------'
- 1 1
? ----
+ 1 1
- + 2 - 9380
? ----
+ + 2 - 9380
- --- ------ ? ----
+ --- ------
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
ERROR - 3:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
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
- ----- ------ ---- ----- ? ----
+ ----- ------ ---- -----
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
ERROR - 4:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
AssertionError: ' 11[31 chars] 1 \n+ 4 - 2999 + 2 + 49 -[51 chars] ' != ' 11[31 chars] 1\n+ 4 - 2999 + 2 + 49 - 938[39 chars]----'
- 11 3801 1 123 1
? ----
+ 11 3801 1 123 1
- + 4 - 2999 + 2 + 49 - 9380
? ----
+ + 4 - 2999 + 2 + 49 - 9380
- ---- ------ --- ----- ------ ? ----
+ ---- ------ --- ----- ------
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
ERROR - 5:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
AssertionError: ' 3 988 \n+ 855 + 40 \n----- ----- \n 858 1028 ' != ' 3 988\n+ 855 + 40\n----- -----\n 858 1028'
- 3 988
? ----
+ 3 988
- + 855 + 40
? ----
+ + 855 + 40
- ----- -----
? ----
+ ----- -----
- 858 1028 ? ----
+ 858 1028
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
ERROR - 6:
PythonError: Traceback (most recent call last):
File "/lib/python311.zip/_pyodide/_base.py", line 468, in eval_code
.run(globals, locals)
^^^^^^^^^^^^^^^^^^^^
File "/lib/python311.zip/_pyodide/_base.py", line 310, in run
coroutine = eval(self.code, globals, locals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<exec>", line 4, in <module>
File "/lib/python311.zip/unittest/case.py", line 873, in assertEqual
assertion_func(first, second, msg=msg)
File "/lib/python311.zip/unittest/case.py", line 1253, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/lib/python311.zip/unittest/case.py", line 703, in fail
raise self.failureException(msg)
AssertionError: ' 3[32 chars] 988 \n- 698 - 3801 + 43 + 49 [100 chars] ' != ' 3[32 chars] 988\n- 698 - 3801 + 43 + 49 + [84 chars]1028'
- 32 1 45 123 988
? ----
+ 32 1 45 123 988
- - 698 - 3801 + 43 + 49 + 40
? ----
+ - 698 - 3801 + 43 + 49 + 40
- ----- ------ ---- ----- -----
? ----
+ ----- ------ ---- ----- -----
- -666 -3800 88 172 1028 ? ----
+ -666 -3800 88 172 1028
at new_error (pyodide.asm.js:9:14992)
at pyodide.asm.wasm:0x152d67
at pyodide.asm.wasm:0x152e6c
at Module.callPyObjectKwargs (pyodide.asm.js:9:75609)
at Module.callPyObject (pyodide.asm.js:9:75818)
at Function.apply (pyodide.asm.js:9:89069)
at Object.apply (pyodide.asm.js:9:87847)
at Object.runPython (pyodide.asm.js:9:122345)
at runPython (python-test-evaluator.ts:121:15)
at test (eval at evaluatedTestString (python-test-evaluator.ts:90:13), <anonymous>:3:5)
at ctx.onmessage (python-test-evaluator.ts:172:11)
Any help would be much appreciated ![]()