Tell us what’s happening:
my program return or prints the exact expected output but except the tests with error output all other tests are failing
Your code so far
def areTooManyProblems(problems):
return len(problems) > 5
def nonAppropriateOperators(problems):
for problem in problems:
operator = problem.split(' ')[1]
if not operator == '+' and not operator == '-':
return True
return False
def isNotInt(problems):
for problem in problems:
[operand1, _, operand2] = problem.split(' ')
if not operand1.strip().isdigit() or not operand2.strip().isdigit():
return True
return False
def isGreaterThan4Digits(problems):
for problem in problems:
[operand1, _, operand2] = problem.split(' ')
if len(operand1.strip()) > 4 or len(operand2.strip()) > 4:
return True
return False
def arithmetic_arranger(problems, show_answers=False):
if areTooManyProblems(problems):
return 'Error: Too many problems.'
if nonAppropriateOperators(problems):
return "Error: Operator must be '+' or '-'."
if isNotInt(problems):
return 'Error: Numbers must only contain digits.'
if isGreaterThan4Digits(problems):
return 'Error: Numbers cannot be more than four digits.'
formatted_problems = ''
first_lines = ''
second_lines = ''
third_lines = ''
fourth_lines = ''
problems_len = len(problems)
for idx, problem in enumerate(problems):
isLastProblem = idx == problems_len - 1
[operand1, operator, operand2] = problem.split(' ')
operand1_len = len(operand1)
operand2_len = len(operand2)
result = f'{int(operand1) - int(operand2)}' if operator == '-' else f'{int(operand1) + int(operand2)}'
if operand1_len > operand2_len:
operand2 = operand2.rjust(operand1_len + 1, ' ')
operand1 = operand1.rjust(operand1_len + 2, ' ')
result = result.rjust(operand1_len + 2, ' ')
else:
operand1 = operand1.rjust(operand2_len + 2, ' ')
operand2 = operand2.rjust(operand2_len + 1, ' ')
result = result.rjust(operand2_len + 2, ' ')
first_lines += operand1 if isLastProblem else operand1.ljust(len(operand1) + 4, ' ')
second_line = operator + operand2
second_lines += second_line if isLastProblem else second_line.ljust(len(second_line) + 4, ' ')
third_line = ('-' * len(operator + operand2))
third_lines += third_line if isLastProblem else third_line.ljust(len(third_line) + 4, ' ')
fourth_lines += result if isLastProblem else result.ljust(len(result) + 4, ' ')
formatted_problems += first_lines + '\\n' + second_lines + '\\n' + third_lines
if show_answers:
formatted_problems += '\\n' + fourth_lines
return formatted_problems
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:129.0) Gecko/20100101 Firefox/129.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project