Tell us what’s happening:
Manual testing of the test criteria results in the same output, however, the coded solution does not pass the tests. I believe my return values are the same as those required and the output to the console indicates so, but the only tests that my code passes are the error tests. Any help would be much appreciated!
Your code so far
def instant_errors(problems):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
if not ("+" in problem or "-" in problem):
return "Error: Operator must be '+' or '-'."
try:
int(problem.replace("+", "").replace("-", ""). replace(" ", ""))
except ValueError:
return "Error: Numbers must only contain digits."
nums = problem.replace("+", "").replace("-", "").split()
if int(nums[0]) > 10000 or int(nums[1]) > 10000:
return "Error: Numbers cannot be more than four digits."
def arithmetic_arranger(problems, show_answers=False):
errors = instant_errors(problems)
first_number = []
operation = []
second_number = []
answers = []
widths = []
first_row = ""
second_row = ""
lines = ""
answers_row = ""
if errors != None:
return errors
else:
for problem in problems:
split_problem = problem.split()
first_number.append(split_problem[0])
operation.append(split_problem[1])
second_number.append(split_problem[2])
for i in range(len(problems)):
widths.append(max(len(str(first_number[i])), len(str(second_number[i]))) + 2)
first_row += " " * (widths[i] - len(str(first_number[i]))) + first_number[i] + " "
second_row += operation [i] + " " * (widths[i] - len(str(second_number[i])) - 1) + second_number[i] + " "
lines += "-" * widths[i] + " "
if operation[i] == "+":
answers.append(int(first_number[i]) + int(second_number[i]))
else:
answers.append(int(first_number[i]) - int(second_number[i]))
answers_row += " " * (widths[i] - len(str(answers[i]))) + str(answers[i]) + " "
if show_answers:
return "\n".join((first_row, second_row, lines, answers_row))
else:
return "\n".join((first_row, second_row, lines))
print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
print()
print(" 3801 123\n- 2 + 49\n------ -----")
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project