1

I wrote some code but I can't get why the pyscripter is telling a syntax error in the if statement:

#search for 9 elements
file_writer = open('C:\\PythonProject2\\commands_NUM.txt','w')
for item in data_indices:
    flag= search_object(item,data,obj_value_min,obj_value_max)
        if flag = True:###ERROR
            file_writer.write('frame0: ' + str(item[0]+1)+' ' + str(item[1]+1) + '\n') ##Here is the syntax error

file_writer.close()

def search_object(pixel,frame,min_val,max_val):
    (obj_y,obj_x) = pixel
    y_center = pixel[0]+1
    x_center = pixel[1]+1
    if(obj_y<=597 and obj_x<=797 ):
        for y in range(0,3):
            for x in range(0,3):
                if((frame[obj_y+y][obj_x+x])<=min_val or(frame[obj_y+y]    [obj_x+x])>=max_val ):
                    return False

return True
1
  • if condition is not properly indented has only one =. Commented Jun 23, 2015 at 9:38

3 Answers 3

4

if statement is indented inwards in for loop. And also = means assignment use == instead

for item in data_indices:
    flag= search_object(item,data,obj_value_min,obj_value_max)
    if flag == True:            #here indent this if one step back
          file_writer.write('frame0: ' + str(item[0]+1)+' ' + str(item[1]+1) + '\n') ##He
Sign up to request clarification or add additional context in comments.

Comments

1

there should be == in if statement

for item in data_indices:
    flag= search_object(item,data,obj_value_min,obj_value_max)
    if flag == True:
        file_writer.write('frame0: ' + str(item[0]+1)+' ' + str(item[1]+1) + '\n')

Comments

1

1) Put your search_object function above - declare it before using it.
2) Fix if flag = True: to if flag:
3) Fix file_writer.write('frame0: ' + str(item[0]+1)+' ' + str(item[1]+1) + '\n') line indentation.
4) return True inside the function search_object is with wrong indentation too. Fix it.
I would recommend you to look at PEP 8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.