Skip to main content
 首页 » 编程设计

python if语句里面有语法错误

2025年05月04日1652php

我写了一些代码,但我不明白为什么 pyscripter 在 if 语句中提示语法错误:

#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 

请您参考如下方法:

if 语句在 for 循环中向内缩进。而且 = 表示赋值使用 == 代替

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