Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New Syntax Error
#1
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")
Reply
#2
It looks like you have a missing close-paren on lines 49 and 53.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,216 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 401 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,629 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,242 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,328 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,273 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 911 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,872 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,379 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,987 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020