Tell us what’s happening:
When i run the tests the formatting looks correct with all the spaces and everything, however i still keeps getting 6 failed tests. It would be amazing if anyone can check out my code and help out thanks!
Your code so far
def arithmetic_arranger(problems, solve = False):
#final solution strings
firstline = ""
secondline = ""
lines = ""
sumdif = ""
strings = ""
if (len(problems) >= 6):
return "Error: Too many problems."
for problem in problems:
first_number = problem.split(" ")[0]
operator = problem.split(" ")[1]
second_number = problem.split(" ")[2]
#operator can only be + or -
if (operator == "/" or operator == "*"):
return "Error: Operator must be '+' or '-'."
#numbers can only be a digit
if (first_number.isdigit() == False or second_number.isdigit() == False):
return "Error: Numbers must only contain digits."
#max of 4 digit width
if (len(first_number) >= 5 or len(second_number) >= 5):
return "Error: Numbers cannot be more than four digits."
# if solve is true solutions
answer = ""
if (operator == "+"):
answer += str(int(first_number) + int(second_number))
elif (operator == "-"):
answer += str(int(first_number) - int(second_number))
#find the length of the longer number
line = "-"
long_length = max((len(first_number)), len(second_number)) + 1
for d in range(long_length):
line += "-"
#returning/printing final values
if problems != problem[-1]:
firstline += first_number.rjust(len(line)) + " "
secondline += operator+ " " + second_number.rjust(len(line)-2) + " "
lines += line + " "
sumdif += answer.rjust(len(line)) + " "
else:
firstline += first_number.rjust(len(line))
secondline += operator + " " +second_number.rjust(len(line)-2)
lines += line
sumdif += answer.rjust(len(line))
if solve:
strings += firstline + "\n" + secondline + "\n" + lines + "\n" + sumdif
else:
strings += firstline + "\n" + secondline + "\n" + lines
print(strings)
return strings
Challenge: Arithmetic Formatter
Link to the challenge: