Tell us what’s happening:
I’ve been puzzling over this for a few days now, it passes the error checks and if i put in the required test by hand the output looks identical to the should return segments but it doesnt pass them in the automated tests?
Your code so far
import re
def arithmetic_arranger(problems, show_answers=False):
#error handling
if len(problems) > 5:
return 'Error: Too many problems.'
numbers = []
for i in problems:
p = i.split()
numbers.extend([p[0], p[2]])
if not all(map(lambda x: x.isdigit(), numbers)):
return "Error: Numbers must only contain digits."
for problem in problems:
if (re.search("['/']", problem) or re.search("['*']", problem)):
return "Error: Operator must be '+' or '-'."
#formatting
first_number = list(map(lambda x: x.split()[0], problems))
operator = list(map(lambda x: x.split()[1], problems))
second_number = list(map(lambda x: x.split()[2], problems))
result = list(map(lambda x: str(eval(x)), problems))
first_number_line = ''
operator_second_number_line = ''
dashes_line = ''
results_line = ''
gap = ' '
for i in range(len(problems)):
#last error check
if len(first_number[i]) > 4 or len(second_number[i]) > 4:
return 'Error: Numbers cannot be more than four digits.'
#formatting
width = max(len(first_number[i]), len(second_number[i])) + 2
first_number_line += first_number[i].rjust(width) + gap
operator_second_number_line += operator[i] + second_number[i].rjust(width - 1) + gap
dashes_line += '-' * width + gap
results_line += result[i].rjust(width) + gap
#show answers flag
if show_answers:
formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line, results_line))
else:
formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line))
return formatted_problems
import re
def arithmetic_arranger(problems, show_answers=False):
#error handling
if len(problems) > 5:
return 'Error: Too many problems.'
numbers = []
for i in problems:
p = i.split()
numbers.extend([p[0], p[2]])
if not all(map(lambda x: x.isdigit(), numbers)):
return "Error: Numbers must only contain digits."
for problem in problems:
if (re.search("['/']", problem) or re.search("['*']", problem)):
return "Error: Operator must be '+' or '-'."
#formatting
first_number = list(map(lambda x: x.split()[0], problems))
operator = list(map(lambda x: x.split()[1], problems))
second_number = list(map(lambda x: x.split()[2], problems))
result = list(map(lambda x: str(eval(x)), problems))
first_number_line = ''
operator_second_number_line = ''
dashes_line = ''
results_line = ''
gap = ' '
for i in range(len(problems)):
#last error check
if len(first_number[i]) > 4 or len(second_number[i]) > 4:
return 'Error: Numbers cannot be more than four digits.'
#formatting
width = max(len(first_number[i]), len(second_number[i])) + 2
first_number_line += first_number[i].rjust(width) + gap
operator_second_number_line += operator[i] + second_number[i].rjust(width - 1) + gap
dashes_line += '-' * width + gap
results_line += result[i].rjust(width) + gap
#show answers flag
if show_answers:
formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line, results_line))
else:
formatted_problems = '\n'.join((first_number_line, operator_second_number_line, dashes_line))
return formatted_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project