Tell us what’s happening:
I think my code is working. It seems to produce all the correct errors and prints the expressions as required yet it is failing all the automated tests.
Your code so far
def check_input_errors(problems):
# Check if there are too many problems
if len(problems) > 5:
print(f'Error: Too many problems.')
return True
for expression in problems:
# Check for invalid operators
if '*' in expression or '/' in expression:
print(f"Error: Operator must be '+' or '-'.")
return True
# Split the expression to check the numbers
parts = expression.split()
if not (parts[0].isdigit() and parts[2].isdigit()):
print(f"Error: Numbers must only contain digits.")
return True
# Check if each number is 4 digits or less
if len(parts[0]) > 4 or len(parts[2]) > 4:
print(f"Error: Numbers cannot be more than four digits.")
return True
return False
def arithmetic_arranger(problems, show_answers=False):
# Run the input error checks first
if check_input_errors(problems):
return
first_line = ""
second_line = ""
dashes = ""
answers = ""
for problem in problems:
parts = problem.split()
first_number = parts[0]
operator = parts[1]
second_number = parts[2]
# Calculate the width of the problem
width = max(len(first_number), len(second_number)) + 2
first_line += first_number.rjust(width) + " "
second_line += operator + second_number.rjust(width - 1) + " "
dashes += "-" * width + " "
if show_answers:
if operator == "+":
answer = str(int(first_number) + int(second_number))
elif operator == "-":
answer = str(int(first_number) - int(second_number))
answers += answer.rjust(width) + " "
# Print the arranged problems
print(first_line.rstrip())
print(second_line.rstrip())
print(dashes.rstrip())
if show_answers:
print(answers.rstrip())
problems = ["24 - 852", "3801 - 2", "45 + 43", "123 + 49", "123 + 49"]
arithmetic_arranger(problems, show_answers=False)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project