Hi everyone. I’m pretty new with python and I’m having trouble with the exit format of the arithmetic problems (as many do). I compared the format with the tests, they look exactly the same, yet I cannot pass the tests =(
Let me share the code of the function (It is also in a link at the end of this message). Please I would appreciate if someone could tell me why I’m getting wrong results. Thanks!
def arithmetic_arranger(problems, show_result=False):
if len(problems)>5:
print('Error: Too many problems.')
quit()
first=[]
sign= []
second= []
spaces=[] #List that saves the maximum spaces for each operation
diff_spaces=[] #list used to accomodate lines 1 and 2 in the exit
result=[ ]
counter=0
for elem in problems:
x=elem.split()
# Convert the elements to int to make math operations
try:
first_int= int(x[0])
sec_int= int(x[2])
except:
print('Error: Numbers must only contain digits.')
quit()
if len(x[0])>4 or len(x[2])>4:
print('Error: Numbers cannot be more than four digits.')
quit()
#The first and second elements as well as the sign go in different lists
first.append(x[0])
sign.append(x[1])
second.append(x[2])
#Tracking the operand with the higher length, to order the output
if len(x[0])>=len(x[2]):
length= len(x[0])
else:
length= len(x[2])
spaces.append(length)
difference= len(x[2])-len(x[0])
diff_spaces.append(difference)
#Math operations and checking for error in the sign
if sign[counter]=='+':
result.append(str(first_int+sec_int))
elif sign[counter]=='-':
result.append(str(first_int-sec_int))
else:
print('Error: Operator must be ''+'' or ''-'' ')
quit()
counter=counter+1
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: