Please help with my code. I don’t know where the problem lies after searching for hours. I keep getting this;
FAILED test_module.py::test_template[test_two_problems_with_solutions]
FAILED test_module.py::test_template[test_five_problems_with_solutions]
Your code so far
def arithmetic_arranger(problems, solver = False):
#1st part
#take second argument which is set to true
#problems should be less than 5. else Error: Too many problems.
#addition and subtraction. Else Error: Operator must be '+' or '-'.
#only numbers. else Error: Numbers must only contain digits.
#only 4 digits in wideth. else Error: Numbers cannot be more than four digits.
#split problems into singles
if len(problems) > 5:
return "Error: Too many problems."
up=""
down=""
lines=""
ans=""
final=""
for problem in problems:
ty=problem.split(" ")
firstnum=ty[0]
operator=ty[1]
secondnum=ty[2]
if len(firstnum) >= 5:
return "Error: Numbers cannot be more than four digits."
elif len(secondnum) >= 5:
return "Error: Numbers cannot be more than four digits."
if firstnum.isdigit() == False:
return "Error: Numbers must only contain digits."
elif secondnum.isdigit() == False:
return "Error: Numbers must only contain digits."
solu=""
if (operator == "+"):
solu = str(int(firstnum) + int(secondnum))
elif (operator == "-"):
solu = str(int(firstnum) - int(secondnum))
elif (operator == "*" or operator == "/"):
return "Error: Operator must be '+' or '-'."
#2nd part
length= max(len(firstnum), len(secondnum)) + 2
#print(length)
top=str(firstnum).rjust(length)
#print(top)
bottom=operator + str(secondnum).rjust(length - 1)
#print(bottom)
answer=str(solu).rjust(length)
#print(answer)
dashes = ""
for dash in range(length):
dashes += "-"
if problem != problems[-1]:
up += top + " "
down += bottom + " "
lines += dashes + " "
ans += answer + " "
else:
up += top
down += bottom
lines += dashes
ans += answer
if solver:
final = up + "\n" + down + "\n" + lines + "\n" + ans
else:
final = up + "\n" + down + "\n" + lines
return final
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: