Tell us what’s happening:
Code seems like it follows all the rules, but doesn’t pass about half the tests. I got it so all the error messages are good, as those just needed periods, but despite the output looking perfect otherwise it won’t count it as passing the tests.
Your code so far
def arithmetic_arranger(problems, show_answers=True):
if len(problems) > 5:
return('Error: Too many problems.')
#Lists of the first and second numbers in math problems
first_number = []
plus_or_minus = []
second_number = []
max_length = []
for problem in problems:
#Splitting the first number, +/-, and second number into different lists
problem_parts = problem.split()
first_number.append(problem_parts[0])
plus_or_minus.append(problem_parts[1])
second_number.append(problem_parts[2])
if problem_parts[1] == '*' or problem_parts[1] == '/':
return("Error: Operator must be '+' or '-'.")
if len(problem_parts[0]) > 4 or len(problem_parts[2]) > 4:
return('Error: Numbers cannot be more than four digits.')
if not (problem_parts[0].isdigit() and problem_parts[2].isdigit()):
return('Error: Numbers must only contain digits.')
#Getting the length of the largest number in the problem so the problem has space
problem_length = max(len(problem_parts[0]), len(problem_parts[2])) + 2
max_length.append(problem_length)
#Making the lines of text for the math problem
first_line = ''
second_line = ''
dashes_line = ''
answer_line = ''
for i in range(len(problems)):
first_line += first_number[i].rjust(max_length[i]) + ' '
second_line += plus_or_minus[i] + second_number[i].rjust(max_length[i] -1) + ' '
dashes_line += '-' * max_length[i] + ' '
if show_answers:
answer = str(eval(problems[i]))
answer_line += answer.rjust(max_length[i]) + ' '
#Put lines together
rearranged_problem = first_line.rstrip() + '\n' + second_line.rstrip() + '\n' + dashes_line.rstrip()
if show_answers:
rearranged_problem += '\n' + answer_line.rstrip()
print(rearranged_problem)
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project