Tell us what’s happening:
My code works as it’s meant to but it only passes tests 5 , 7 and 8.
What am I meant to do to earn my certification? Has anybody actually gotten it?
Your code so far
def arithmetic_arranger(problems, show_answers=True):
line1 = ' '
line2 = ' '
line_equal = ' '
line_result = ' '
# Check if there are too many problems
if len(problems) > 5:
return 'Error: Too many problems.'
# Process each problem
for problem in problems:
parts = problem.split()
num1 = parts[0]
operator = parts[1]
num2 = parts[2]
# Validate the operator
if operator not in ['+', '-']:
return 'Error: Operator must be "+" or "-".'
# Validate the length of the numbers
if len(num1) > 4 or len(num2) > 4:
return 'Error: Numbers cannot be more than four digits.'
# Validate that the numbers contain only digits
if not num1.isdigit() or not num2.isdigit():
return 'Error: Numbers must only contain digits.'
# Calculate the result if required
if show_answers:
if operator == '+':
result = str(int(num1) + int(num2))
elif operator == '-':
result = str(int(num1) - int(num2))
# Determine the maximum length for formatting
max_len = max(len(num1), len(num2)) + 2
# Format the lines
line1 += num1.rjust(max_len) + ' ' # 4 spaces between problems
line2 += operator + ' ' + num2.rjust(max_len - 2) + ' '
line_equal += '-' * max_len + ' '
# Add the result line if required
if show_answers:
line_result += result.rjust(max_len) + ' '
# Combine the lines into the final formatted string
if show_answers:
formatted_problems = str(line1.rjust(max_len) + '\n' + line2.rjust(max_len) + '\n' + line_equal + '\n' + line_result.rjust(max_len))
else: #if no result is required
formatted_problems = str(line1.rjust(max_len) + '\n' + line2.rjust(max_len) + '\n' + line_equal)
return formatted_problems
print(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