for line in lines[lines.index(Checkstart) + 1:lines.index(Checkstart+'complete')]: if…
- User
for line in lines[lines.index(Check_start) + 1:lines.index(Check_start+'_complete')]: if 'Read' in line: setup = line.split('/') ser.write(f'{setup[0]}{setup[1]}%'.encode()) read_val = read_string(ser) log.insert(tk.END, read_val) print('read') '''elif 'YES' in line: Path_YES = line.split(":")[1] elif 'NO' in line: Path_NO = line.split(":")[1]''' elif 'YES' in line: print('cont read') '''Stop = line.split(":")[1] print(Stop) if Stop == 'NO': #Check_start = Path_goto print('Check_start:' + Check_start) else: print('Stop') break'''
File "C:.Workplace\Python\OTP_programmer 2.0\func.py", line 351 elif 'YES' in line: ^^^^ SyntaxError: invalid syntax?
why
- Assistant
The error message you're encountering indicates a
SyntaxErrorbecause theelifstatement is not properly placed in the context of its precedingifstatement. In Python, anelifmust follow anifstatement, and it looks like it directly follows a comment block (the multiline comment''' ... ''') instead of anifcondition.Here's a revised version of your code to fix the syntax issue. Simply remove the comment block or adjust the positioning of the
elifstatement correctly:for line in lines[lines.index(Check_start) + 1:lines.index(Check_start + '_complete')]: if 'Read' in line: setup = line.split('/') ser.write(f'{setup[0]}{setup[1]}%'.encode()) read_val = read_string(ser) log.insert(tk.END, read_val) print('read') elif 'YES' in line: print('cont read') # Uncomment or revise the following lines if needed. # Stop = line.split(":")[1] # print(Stop) # if Stop == 'NO': # # Check_start = Path_goto # print('Check_start:' + Check_start) # else: # print('Stop') # break - User
################ def Check_procedure(): global product_data,file_data,timeout,CRC_ENABLE,status,thread_running,validate_no_tm_flg lines= product_data.splitlines() Check_start = 'Check_main' read_val='' Compare_val ='' Path_YES = '' Path_NO = '' Path_goto = '' Cont = 'run' Compare_result = '' while Cont == 'run': for line in lines[lines.index(Check_start) + 1:lines.index(Check_start+'_complete')]: if 'Read' in line: setup = line.split('/') ser.write(f'{setup[0]}{setup[1]}%'.encode()) read_val = read_string(ser) log.insert(tk.END, read_val) print('read') elif 'YES' in line: Path_YES = line.split(":")[1] elif 'NO' in line: Path_NO = line.split(":")[1] elif 'Cont' in line: print('cont read') Cont = line.split(":")[1] print(Cont) if Cont == 'run': Check_start = Path_goto print('Check_start:' + Path_NO) else: print('Stop') break elif 'Compare' in line: Compare_val = line.split(":")[1] if read_val == Compare_val: Path_goto = Path_YES else: Path_goto = Path_NO print('Path_goto' + Path_goto) else: print('else') setup = line.split('/') ser.write(f'{setup[0]}{setup[1]}%'.encode()) log_response(ser) print(Check_start)
for this code the for loop have a variable check_start. in the for loop I have a bunch of elif. which will read the lines from a txt file and change the check_start variable. then I would like the for loop to be changed to read the new check_start line. the loop is not being changed, though check start variable have already changed. how to fix that
- Assistant
- User
this is also not switching
- Assistant
- User
so this code i have is in the function script. I have have my main. py
I like to have another elif in the there elif 'warn' in line: warning message= line.split(":")[1]
and the warning message will pop up
- Assistant