Tell us what’s happening:
Hello, I’m not sure what’s wrong with my code, 4 passed and 6 failed
test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] FAILED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] FAILED [100%]
Your code so far
import re
def arithmetic_arranger(problems, result = False):
if(len(problems) > 5):
return "Error: Too many problems."
x = ""
y = ""
lines = ""
sumx = ""
string = ""
for problem in problems:
if(re.search("[^\s0-9.+-]", problem)):
if(re.search("[/]", problem) or re.search("[*]", problem)):
return "Error: Operator must be '+' or '-'."
return "Error: Numbers must only contain digits."
firstNum = problem.split(" ")[0]
operator = problem.split(" ")[1]
secondNum = problem.split(" ")[2]
if(len(firstNum) >= 5 or len(secondNum) >=5):
return "Error: Numbers cannot be more than four digits."
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 = operator + str(secondNum).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range(length):
line += "-"
if problem != problems[-1]:
x += top + ' '
y += bottom + ' '
lines += line + ' '
sumx += res + ' '
else:
x += top
y += bottom
lines += line
sumx += res
if result:
string = x +"\n"+ y +"\n"+ lines +"\n"+ sumx
else:
string = x +"\n"+ y +"\n"+ lines
return string
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: