I am stuck with the project for 2 days as I cannot pass the test cases, my output is identical. Here’s my code,
def arithmetic_arranger(problems, solve=False):
if (len(problems) > 5):
return "Error: Too many problems."
first = ""
second = ""
lines = ""
sumf = ""
string = ""
for p in problems:
firstNum = p.split(" ")[0]
operator = p.split(" ")[1]
secondNum = p.split(" ")[2]
if (not firstNum.isnumeric() or not secondNum.isnumeric()):
return "Error: Numbers must only contain digits."
if len(firstNum) > 4 or len(secondNum) > 4:
return "Error: Numbers cannot be more than four digits."
if (operator == "/" or operator == "*"):
return "Error: Operator must be '+' or '-'."
sum = ""
if operator == "+":
sum = str(int(firstNum) + int(secondNum))
elif operator == "-":
sum = str(int(firstNum) - int(secondNum))
length = max(len(firstNum), len(secondNum)) + 2
top = str(firstNum).rjust(length)
bottom = str(operator) + str(secondNum).rjust(length - 1)
line = ""
ans = str(sum).rjust(length)
for l in range(length):
line += "-"
if p != p[-1]:
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumf += ans + ' '
else:
first += top
second += bottom
lines += line
sumf += ans
if solve:
string = first + "\n" + second + "\n" + lines + "\n" + sumf
else:
string = first + "\n" + second + "\n" + lines
return string
hi Nathan, this is a certification project. You have to be careful how much help you give especially when no specific question has been asked. (otherwise it appears to be bordering on cheating)
Thank you, Yes my formatting conditional statement was the one with the issue as I needed to do p != problems[-1] instead of p != p[-1]. Thank you again for the help.