gpt4 book ai didi

python if语句里面有语法错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:06:06 24 4
gpt4 key购买 nike

我写了一些代码,但我不明白为什么 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

关于python if语句里面有语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com