Syntax error in python 2.3.4

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akilasekaran
    New Member
    • Feb 2012
    • 15

    Syntax error in python 2.3.4

    i am new to the python codes.. i just started out hello world.. i ended up getting an "invalid syntax" error... but i was able to run a TCP server program from the very same version of python which is python 2.3.4

    Iam perplexed !!
  • cemyildiz
    New Member
    • Feb 2012
    • 5

    #2
    Can you share your code here?

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      It could be something as simple as missing quotes.
      Code:
      >>> print hello world
      Traceback (  File "<interactive input>", line 1
          print hello world
                          ^
      SyntaxError: invalid syntax
      >>> print "hello world"
      hello world
      >>>

      Comment

      • akilasekaran
        New Member
        • Feb 2012
        • 15

        #4
        D:\Python23>pyt hon hii.py
        File "hii.py", line 1
        Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win
        32
        ^
        SyntaxError: invalid syntax


        I got this when i tried to run the program from cmd line.

        Comment

        • cemyildiz
          New Member
          • Feb 2012
          • 5

          #5
          try to put quatos after print inside of hii.py:

          print "hello world"

          Comment

          • akilasekaran
            New Member
            • Feb 2012
            • 15

            #6
            i still get the same error :-(

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Please post the code you are using.

              Comment

              • akilasekaran
                New Member
                • Feb 2012
                • 15

                #8
                >>> h="hello world"
                >>> print h
                hello world

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Remove the ">" characters and leading spaces. Your file should contain only:
                  Code:
                  h="hello world"
                  print h

                  Comment

                  • akilasekaran
                    New Member
                    • Feb 2012
                    • 15

                    #10
                    not only this program , but in other basic programs also i get the same syntax error.. but no error comes in a this one.. i wonder how..

                    Code:
                    #Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
                    #Type "copyright", "credits" or "license()" for more information.
                    #import socket module
                    from socket import *    
                    serverSocket = socket(AF_INET, SOCK_STREAM)
                    #Prepare a sever socket
                    host=gethostbyname(gethostname())
                    print host
                    port=1234
                    serverSocket.bind((host,port))
                    while True:
                      print 'Ready to serve...'
                      serverSocket.listen(1)
                      connectionSocket,addr=serverSocket.accept()
                      try:
                    	message = connectionSocket.recv(4096)   
                    	filename = message.split()[1]  
                    	f = open(filename[1:])  
                            outputdata = f.read()
                    	#Send one HTTP header line into socket
                        #Fill in start
                            connectionSocket.send("200 OK") 
                        #Fill in end  
                        #Send the content of the requested file to the client
                            for i in range(0, len(outputdata)):  
                               connectionSocket.send(outputdata[i])
                            connectionSocket.close()
                      except IOError:
                         #Send response message for file not found
                         #Fill in start
                         connectionSocket.send("404 file not found")
                         #Fill in end
                         #Close client socket
                         #Fill in start
                         connectionSocket.close()
                         #Fill in end  
                    serverSocket.close()
                    Last edited by bvdet; Feb 12 '12, 04:32 PM. Reason: Please use code tags when posting code [code]....[/code]

                    Comment

                    • akilasekaran
                      New Member
                      • Feb 2012
                      • 15

                      #11
                      when i type in the python GUI i am unable to remove the >>>

                      may b am troublin u a lot.. :-D

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        ">>> " belongs in the interactive window but not in the file being executed.

                        It's strange that the code you posted works. It has several indentation errors.

                        Comment

                        • akilasekaran
                          New Member
                          • Feb 2012
                          • 15

                          #13
                          actually the hello world program worked successfull when i did it without >>> .Thanks a lot.. but now another problem..

                          This program i wrote for server.
                          Code:
                          from socket import *
                          s = socket(AF_INET,SOCK_STREAM) 
                          h=gethostbyname(gethostname())
                          print'host is ',h
                          s.bind((h,9000))
                          s.listen(1)
                          while 1:
                              try:
                              client, address = s.accept()
                              print'connection from',address
                              data = client.recv(1024)
                              if not data:break 
                                  client.send(data) 
                              client.close()
                              
                              except IOError:
                                  print'IOError is there !'
                                  client.close()
                          i get this following error :

                          D:\Python23>pyt hon serv.py
                          File "serv.py", line 10
                          print"connectio n from",address
                          ^
                          SyntaxError: invalid syntax
                          Last edited by bvdet; Feb 12 '12, 04:51 PM. Reason: Please use code tags when posting code [code]....[/code]

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            Please use code tags when posting code. See "Need help with Bytes? (FAQ)"

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Your code is not indented properly. The interpreter expects an indentation after a try statement.

                              Comment

                              Working...