Tell us what’s happening:
Hi,
My code is failing the last two test, and I am confused because I am getting the “correct” output in my computer terminal.
These two:
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]
General Failure summary:
==================== FAILURES ====================
_ test_template[test_two_problems_with_solutions] _
arguments = [[‘3 + 855’, ‘988 + 40’], True]
expected_output = ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with [“3 + 855”, “988 + 40”] and a second argument of True
.’
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
assert actual == expected_output, fail_message
E AssertionError: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with [“3 + 855”, “988 + 40”] and a second argument of True
.
E assert ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028 ’ == ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
E 3 988
E + 855 + 40
E ----- -----
E - 858 1028
E + 858 1028
E ? ++++
test_module.py:77: AssertionError
_ test_template[test_five_problems_with_solutions] _
arguments = [[‘32 - 698’, ‘1 - 3801’, ‘45 + 43’, ‘123 + 49’, ‘988 + 40’], True]
expected_output = ’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028’
fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True
.’
@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
actual = arithmetic_arranger(*arguments)
assert actual == expected_output, fail_message
E AssertionError: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True
.
E assert ’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028 ’ == ’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028’
E 32 1 45 123 988
E - 698 - 3801 + 43 + 49 + 40
E ----- ------ ---- ----- -----
E - -666 -3800 88 172 1028
E + -666 -3800 88 172 1028
E ? ++++
test_module.py:77: AssertionError
Your code so far
Sorry that my I wrote a lot to give a solution to this problem!
def arithmetic_arranger(list, answers=False):
max_problems = 5
if amount_problems(list) > max_problems:
return ‘Error: Too many problems.’
if check_operator(list):
return “Error: Operator must be ‘+’ or ‘-’.”
if my_is_digit(list):
return ‘Error: Numbers must only contain digits.’
if max_four_digits(list):
return ‘Error: Numbers cannot be more than four digits.’
else:
final_str = ‘-1’
line_1_list =
line_2_list =
line_3_list =
my_max_list = []
my_max_list = max_length_list(list)
line_1_list = populate_line_1(list, my_max_list)
line_2_list = populate_line_2(list, my_max_list)
line_3_list = populate_line_3(my_max_list)
line_1_str = ''
line_2_str = ''
line_3_str = ''
four_spaces = ' '
counter = 0
for _ in range(len(list)):
if counter == (len(list) - 1):
line_1_str += line_1_list[counter]
line_2_str += line_2_list[counter]
line_3_str += line_3_list[counter]
else:
line_1_str += line_1_list[counter] + four_spaces
line_2_str += line_2_list[counter] + four_spaces
line_3_str += line_3_list[counter] + four_spaces
counter += 1
final_str = line_1_str + '\n' + line_2_str + '\n' + line_3_str
if answers:
answer_list = []
answer_list = populate_answers_line(list)
answer_list_format = populate_answers_format(answer_list, my_max_list)
counter_2 = 0
answer_str = ''
for str_ in answer_list_format:
if counter_2 == len(answer_list_format):
answer_str += str_
else:
answer_str += str_ + four_spaces
final_str += '\n' + answer_str
return final_str
else:
return final_str
This function return a int which represents the number of problems in the list. If the number is greater than four the error message will be executed it.
def amount_problems(list):
count = 0
for i in list:
count += 1
return count
This function returns a True boolean if the operator is not ‘*’ or ‘/’, so it will activate the error message because this program will execute only ‘+’ or ‘-’ operations.
def check_operator(list):
for str_ in list:
first_term, operator_, sec_term = str_.split()
if operator_ == ‘*’ or operator_ == ‘/’ or operator_ == ‘//’:
return True
return False
This function returns a boolean, True, if a string from the list is not a number, so the error message will be activate from the method that called it.
def my_is_digit(list):
for str_ in list:
first_term, operator_, sec_term = str_.split()
if (not first_term.isdigit()) or (not sec_term.isdigit()):
return True
return False
This function returns a True boolean if any string from the list contains more than four characteres. Then, it will activate the error message if the length of any string is greater than four.
def max_four_digits(list):
max_len = 4
for str_ in list:
first_term, operator_, sec_term = str_.split()
if len(first_term) > max_len or len(sec_term) > max_len:
return True
return False
This function returns a list with the appropiate formatter for the FIRST line.
def populate_line_1(list, my_max_list):
temp_list =
count_ = 0
for str_ in list:
str_head = ’ ’
first_term, operator_, sec_term = str_.split()
if len(first_term) < my_max_list[count_]: # e.i.: 32 + 698
space_to_add = my_max_list[count_] - len(first_term)
for _ in range(space_to_add):
str_head += ’ ’
str_head += first_term
temp_list.append(str_head)
else: # e.i.: 3801 - 2 or 45 - 43
str_head += first_term
temp_list.append(str_head)
count_ += 1
return temp_list
This function returns a list with the appropiate formatter for the SECOND line.
def populate_line_2(list, my_max_list):
temp_list =
count_ = 0
for str_ in list:
first_term, operator_, sec_term = str_.split()
str_head = operator_ + ’ ’
if len(sec_term) < my_max_list[count_]: # e.i: 3801 - 2
space_to_add = my_max_list[count_] - len(sec_term)
for _ in range(space_to_add):
str_head += ’ ’
str_head += sec_term
temp_list.append(str_head)
else:
str_head += sec_term
temp_list.append(str_head)
count_ += 1
return temp_list
This function returns a list with the appropiate formatter for the THIRD line which is a line with the dashed format.
def populate_line_3(my_max_list):
temp_list =
adding_two_space = 2
for num in my_max_list:
temp_num = int(num)
dash_str = ‘’
for _ in range(temp_num + adding_two_space):
dash_str += ‘-’
temp_list.append(dash_str)
return temp_list
This function returns a list with the greatest value for each operation.
def max_length_list(list):
my_list_return =
for str_ in list:
first_term, operator_, sec_term = str_.split()
if len(first_term) > len(sec_term):
my_list_return.append(len(first_term))
else:
my_list_return.append(len(sec_term))
return my_list_return
This function returns a list containing the answer for each desired operation.
def populate_answers_line(list):
temp_list =
for str_ in list:
first_term, operator_, sec_term = str_.split()
if operator_ == ‘+’:
sum = int(first_term) + int(sec_term)
temp_list.append(sum)
if operator_ == ‘-’:
substraction = int(first_term) - int(sec_term)
temp_list.append(substraction)
return temp_list
This function returns a formatted list of all the answers to join the last answer if answers were asked to be shown
def populate_answers_format(answer_list, my_max_list):
counter = 0
temp_list =
for str_ in answer_list:
temp_str = str(str_)
max_len_line = my_max_list[counter] + 2
space_add = max_len_line - len(temp_str)
tmp_str_add = ‘’
for _ in range(space_add):
tmp_str_add += ’ ’
tmp_str_add += temp_str
temp_list.append(tmp_str_add)
counter += 1
return temp_list
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: