Tell us what’s happening:
Everything was right in console until I submitted it, I failed all the tests except error handlers
Your code so far
def arithmetic_arranger(problems, show_answers=False):
# Set-ups
required_operators = ['+', '-']
max_digits = 4
max_problems = 5
distance = ' ' * 4
# Variables
first_line = ''
second_line = ''
dashes_line = ''
# Limit the amount of problems
if len(problems) > max_problems:
return 'Error: Too many problems.'
# Iterate through problems to find errors and arrange them
for pr in problems:
operands = pr.split()[::2]
operator = pr.split()[1]
# Handle rule-breaks
for op in operands:
if not op.isdigit() and not op.isspace():
return 'Error: Numbers must only contain digits.'
elif len(op) > max_digits:
return 'Error: Numbers cannot be more than four digits.'
if operator not in required_operators:
return "Error: Operator must be '+' or '-'."
# Arrange problems on 3 lines
longer_side = 0 if len(operands[0]) > len(operands[1]) else 1
shorter_side = abs(longer_side - 1)
extra_space = ' ' * (len(operands[longer_side]) - len(operands[shorter_side]))
# FIRST LINE
first_line += (' '
+ (extra_space if longer_side == 1 else '')
+ operands[0] + distance)
# SECOND LINE
second_line += (operator + ' '
+ (extra_space if longer_side == 0 else '')
+ operands[1] + distance)
# DASHES LINE
dashes_line += '-' * (2 + len(operands[longer_side])) + distance
if show_answers:
print(f'{first_line}\n{second_line}\n{dashes_line}')
return f'{first_line}\n{second_line}\n{dashes_line}'
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/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project