Tell us what’s happening:
I’m trying to finish the [Scientific Computing with Python][Scientific Computing with Python Projects] and my unit test keep get failures. But when i test it manualy it works fine.
And i cant identify what is the diference. Can some on help me?
Your code so far
def arithmetic_arranger(problems, res=False):
num1 =
num2 =
width =
op =
results =
if len(problems) > 5:
return print("Error: Too many problems.")
for problem in problems:
problem = problem.split()
if problem[1] == '*' or problem[1] == '/':
return print("Error: Operator must be '+' or '-'.")
try:
int(problem[0])
int(problem[2])
except:
return print("Error: Number must only contain digits.")
if len(problem[0]) > 4:
return print("Error: Numbers cannot be more than four digits.")
elif len(problem[2]) > 4:
return print("Error: Numbers cannot be more than four digits.")
if len(problem[0]) > len(problem[2]):
width.append(len(problem[0])+2)
else:
width.append(len(problem[2])+2)
num1.append(problem[0])
num2.append(problem[2])
op.append(problem[1])
if res is True:
if problem[1] == '+':
results.append(int(problem[0]) + int(problem[2]))
else:
results.append(int(problem[0]) - int(problem[2]))
arranged_problems = ""
count = 0
for n in num1:
dif = width[count] - len(num1[count])
if count == 0:
arranged_problems = ' '*dif + n
count +=1
else:
arranged_problems += ' ' * (dif + 4) + n
count += 1
second_line = []
count = 0
while count < len(op):
dif = width[count] - len(num2[count]) - 1
if len(second_line) == 0:
second_line = op[count] + ' ' * dif
second_line = second_line + num2[count]
count += 1
else:
second_line = second_line + ' ' + op[count] + ' ' * dif
second_line = second_line + num2[count]
count += 1
third_line = []
count = 0
while count < len(num1):
if len(third_line) == 0:
third_line = '-' * width[count]
count += 1
else:
third_line = third_line + ' ' + '-' * width[count]
count += 1
result_line = []
count = 0
for result in results:
dif = width[count] - len(str(result))
if count == 0:
result_line = ' ' * dif + str(result)
count += 1
else:
result_line += ' ' * (dif + 4) + str(result)
count += 1
if res == True:
arranged_problems += str(f'\n{second_line}\n{third_line}\n{result_line}')
else:
arranged_problems += str(f'\n{second_line}\n{third_line}')
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: