Tell us what’s happening:
My code is doing what it is supposed to do in the preview window but when i run tests only the tests that is needed to raise an error passes while all other tests that should preview an answer fails
Your code so far
def arithmetic_arranger(problems, show_answers=False):
#validating the limit of problems count
if len(problems) > 5:
return "Error: Too many problems."
#setting up initial rows strings
first_row = ""
second_row = ""
third_row = ""
forth_row = ""
for problem in problems:
#validating that the problem has only 3 parts
if len(problem.split(" ")) == 3:
pass
else:
return "Error: Each problem should follow the formate (number1 +OR- number2)"
number1 = problem.split(" ")[0]
operator = problem.split(" ")[1]
number2 = problem.split(" ")[2]
#validating the operator sign
if operator not in ("+", "-"):
return "Error: Operator must be '+' or '-'."
#validating that numbers only contain digits
if is_int(number1):
if is_int(number2):
pass
else:
return 'Error: Numbers must only contain digits.'
else:
return 'Error: Numbers must only contain digits.'
#validating that each number contains 4 digits or less
if len(number1) <= 4:
if len(number2) <= 4:
pass
else:
return 'Error: Numbers cannot be more than four digits.'
else:
return 'Error: Numbers cannot be more than four digits.'
#adjusting formate
if len(number1) >= len(number2):
space = len(number1) - len(number2)
first_row += " "*2 + number1 + " "*4
second_row += operator + " " + " "*space + number2 + " "*4
third_row += "--" + "-"*len(number1) + " "*4
total = int(number1) + int(number2)
if len(str(total)) > len(number1):
forth_row += " " + str(total) + " "*4
else:
forth_row += " "*2 + str(total) + " "*4
else:
space = len(number2) - len(number1)
first_row += " "*2 + " "*space + number1 + " "*4
second_row += operator + " " + number2 + " "*4
third_row += "--" + "-"*len(number2) + " "*4
total = int(number1) + int(number2)
if len(str(total)) > len(number2):
forth_row += " " + str(total) + " "*4
else:
forth_row += " "*2 + str(total) + " "*4
if show_answers:
new_problems = first_row+'\n'+second_row+'\n'+third_row+'\n'+forth_row
else:
new_problems = first_row+'\n'+second_row+'\n'+third_row
return new_problems
def is_int(number):
if number[0] in ('-', '+'):
return number[1:].isdigit()
return number.isdigit()
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project