Tell us what’s happening:
I was doing the first certification project and everything was doing well. I started testing everything that the project requires and all worked as expected but when I run the tests to aprove the project the test number 5 where when putting more than 5 problems it should return “Error: Too many problems” isn’t checked but after I tested it by myself, worked well. What should I do?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
# Check the number of problems
if len(problems)>5:
return('Error: Too many problems')
first_line=[]
second_line=[]
dashes = []
answers = []
for problem in problems:
parts = problem.split()
# Check if the operator is valid
if parts[1] not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
# Check if operands are digits
if not parts[0].isdigit() or not parts[2].isdigit():
return "Error: Numbers must only contain digits."
# Check if operands are at most four digits
if len(parts[0]) > 4 or len(parts[2]) > 4:
return "Error: Numbers cannot be more than four digits."
# Calculate the width of the problem
width = max(len(parts[0]), len(parts[2])) + 2
# Format each part
first_line.append(parts[0].rjust(width))
second_line.append(parts[1] + " " + parts[2].rjust(width - 2))
dashes.append('-' * width)
# Calculate the answer if required
if show_answers:
if parts[1] == '+':
result = str(int(parts[0]) + int(parts[2]))
else: # subtraction
result = str(int(parts[0]) - int(parts[2]))
answers.append(result.rjust(width))
# Join each line with four spaces between problems
arranged_problems = (
" ".join(first_line) + "\n" +
" ".join(second_line) + "\n" +
" ".join(dashes)
)
# Add answers if show_answers is True
if show_answers:
arranged_problems += "\n" + " ".join(answers)
return arranged_problems
# Test 1
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
# Test 2
print(f'\n{arithmetic_arranger(["1 + 2", "1 - 9380"])}')
# Test 3
print(f'\n{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
# Test 4
print(f'\n{arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"])}')
# Test 5
print(f'\n{arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"])}')
# Test 6
print(f'\n{arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])}')
# Test 7
print(f'\n{arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"])}')
# Test 8
print(f'\n{arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"])}')
# Test 9
print(f'\n{arithmetic_arranger(["3 + 855", "988 + 40"], True)}')
# Test 10
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project