Tell us what’s happening:
I have done everything as told in the problem description and i am getting the correct answers as and the console shows my print statements use the proper format but i still get the e errors
FAIL: test_arrangement (test_module.UnitTests)
FAIL: test_solutions (test_module.UnitTests)
Your code so far
def arithmetic_arranger(problems,answer=False):
import re
split_probs=[]
i=0
numbers=[]
operands=[]
answers=[]
for problem in problems:
split_probs.append(problem.split())
#check if too many problems
if len(problems)>5:
prob1="Error: Too many problems."
print(prob1)
cond1=False
return prob1
else:
cond1=True
#check no multiple or division
for problem in problems:
if re.search('(/|\*)',problem):
prob2="Error: Operator must be '+' or '-'."
print(prob2)
cond2=False
return prob2
else:
cond2=True
#make sure only digits
cond3=True
for problem in split_probs:
i=0
if cond3==True:
while i<3:
if problem[i].isnumeric() or problem[i]=='+' or problem[i]=='-':
cond3=True
i=i+1
else:
cond3=False
prob3="Error: Numbers must only contain digits."
print(prob3)
return prob3
else:
break
#check digits less than 5
cond4=True
i=0
for problem in split_probs:
if cond4==True:
i=0
while i<3:
if len(problem[i])>4:
prob4="Error: Numbers cannot be more than four digits."
print(prob4)
cond4=False
return prob4
else:
cond4=True
i=i+1
else:
break
if (cond1 and cond2 and cond3 and cond4)==True:
if answer==True:
countnum=0
countop=0
for inp in split_probs:
i=0
while i<3:
if inp[i].isnumeric():
numbers.append(inp[i])
i=i+1
else:
operands.append(inp[i])
i=i+1
while countop<len(operands):
num1=numbers[countnum]
countnum=countnum+1
num2=numbers[countnum]
if operands[countop]=='-':
num3=int(num1)-int(num2)
answers.append(num3)
countop=countop+1
countnum=countnum+1
else:
num3=int(num1)+int(num2)
answers.append(num3)
countop=countop+1
countnum=countnum+1
botlin=''
toplin=''
offset=0
thirdlin=''
forthlin=''
cnt=0
for parts in split_probs:
if len(parts[0])>(len(parts[2])):
offset=len(parts[0])-len(parts[2])-1
toplin=toplin+" {0} ".format(parts[0])
botlin=botlin + "{0}".format(parts[1]) + " "*offset +" {0} ".format(parts[2])
thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+1)+" "
if answer==True:
answeroff=len(parts[0])+1-len(str(answers[cnt]))
forthlin=forthlin+" "*answeroff +"{0} ".format(answers[cnt])
cnt=cnt+1
elif len(parts[0])<(len(parts[2])):
offset=len(parts[2])+2-len(parts[0])
toplin=toplin+ " "*offset + "{0} ".format(parts[0])
botlin=botlin+"{0} {1} ".format(parts[1],parts[2])
thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+2)+" "
if answer==True:
answeroff=len(parts[2])+2-len(str(answers[cnt]))
forthlin=forthlin+" "*answeroff+"{0} ".format(answers[cnt])
cnt=cnt+1
else:
toplin=toplin+" {0} ".format(parts[0])
botlin=botlin + "{0} {1} ".format(parts[1],parts[2])
thirdlin=thirdlin+'-'*(max(len(parts[0]),len(parts[2]))+2)+" "
if answer==True:
answeroff=len(parts[0])+2-len(str(answers[cnt]))
forthlin=forthlin+" "*answeroff+ "{0} ".format(answers[cnt])
cnt=cnt+1
print(toplin)
print(botlin)
print(thirdlin)
print(forthlin)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: