Python Forum

Full Version: New Syntax Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new to python and have written the following binary search and it was running then suddenly I am getting a syntax error on line 58. I am running Visual Studio Code and have installed python and anaconda extensions. Also I am getting a new syntax error at the "except" statement (line 56). This program takes an required zip code and searches a text file until it finds a match. Then it sets S1 and S2. I have attached the code here. Can anyone help please? Thanks

from tkinter import *
from pathlib import Path
import os.path

Zip_Req=37075
Zip_Cd=[]

try:
    with open("C:\MezzDesign2018\ZipCodes.txt") as Zip_Cds:
        i=0
        while True :
            ZipCd=Zip_Cds.readline()[:5]          
            if not ZipCd:
                break
            else:
                Zip_Cd.append(ZipCd)
                i=i+1               
        Zip_Cds.close
        i=i-1
        n_end=i#.splitlines())        
        n_strt=1        
        ZipCdStrt=int(Zip_Cd[n_strt])
        ZipCdEnd=int(Zip_Cd[n_end])
        i_strt=n_strt
        i_end=i
        print(i-1,Zip_Cd[2],Zip_Cd[i-1])
        while int(Zip_Cd[i]) is not int(Zip_Req):
            if int(ZipCdStrt)<int(Zip_Req):
                if int(ZipCdEnd)>int(Zip_Req):
                    n_s=n_strt
                    n_e=n_end
                    n_end=int((n_strt+n_end)/2)                   
                    ZipCdEnd=int(Zip_Cd[n_end])
            if int(ZipCdStrt)>int(Zip_Req):
                 if int(ZipCdEnd)>int(Zip_Req):
                      ZipCdEnd=ZipCdStrt
                      n_end=n_strt 
                      n_e=n_end                    
                      n_strt=n_s
                      ZipCdStrt=int(Zip_Cd[n_strt])
            if int(ZipCdStrt)<int(Zip_Req):
                 if int(ZipCdEnd)<int(Zip_Req):
                      ZipCdStrt=ZipCdEnd
                      n_strt=n_end
                      n_end=n_e
                      ZipCdEnd=int(Zip_Cd[n_end])
            if int(ZipCdStrt) is int(Zip_Req):                              
                S1=Value(Zip_Cds.readline()[5:19])
                S2=Value(Zip_Cds.readline()[19:30]
               
            if int(ZipCdEnd) is int(Zip_Req):                              
                S1=Value(Zip_Cds.readline()[5:19])
                S2=Value(Zip_Cds.readline()[19:30]  
            
               
except FileNotFoundError:
    print("File ZipCodes.txt Does Not Exist")
    
print("Loops have completed")
It looks like you have a missing close-paren on lines 49 and 53.