Tell us what’s happening:
Preview looks correct, but code is still not passing the tests. Thought the issue was trailing white spaces, but even after removing those (I think I did at least) it’s still not passing. Not sure what the reason it isn’t passing is.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
#CHANGE THIS BACK TO FALSE BEFORE SUBMISSION
line1 = ''
line2 = ''
line3 = ''
line4 = ''
#the final strings to return at the end
if len(problems) > 5:
return "Error: Too many problems."
#Handles problem limit error
for problem in problems:
problem_tuple = problem.split(" ")
digit1 = problem_tuple[0]
function = problem_tuple[1]
digit2 = problem_tuple[2]
#split and set variables
if len(digit1) > 4 or len(digit2) > 4:
return "Error: Numbers cannot be more than four digits."
#handle length error
if problem_tuple[1] == "/" or problem_tuple[1] == "*":
return "Error: Operator must be '+' or '-'."
#handles operator error
if problem == problems[-1]:
seperator = ""
else:
seperator = " "
#Spaces to seperate each column, sets to none if at the last problem
width = max(len(digit1), len(digit2)) + 2
#Show how much empty space is required to line up the results, if on the last problem set width to 0
line1 += " " * (width - len(digit1)) + digit1 + seperator
line2 += function + " " * (width - len(digit2) - 1) + digit2 + seperator
line3 += ("-" * width) + seperator
#Create first 3 lines
if problem_tuple[1] == "+":
result = int(problem_tuple[0]) + int(problem_tuple[2])
if problem_tuple[1] == "-":
result = int(problem_tuple[0]) - int(problem_tuple[2])
line4 += (" " * (width - len(str(result)))) + str(result) + seperator
#Create the 4th line, depending on the operator
if show_answers:
return line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
else:
return line1 + "\n" + line2 + "\n" + line3
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/134.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project