By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

Syntax error in python 2.3.4

P: 15
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 !!
Feb 12 '12 #1

✓ answered by bvdet

">>> " 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.

Share this Question
Share on Google+
21 Replies

P: 5
Can you share your code here?
Feb 12 '12 #2

bvdet
Expert Mod 2.5K+
P: 2,851
It could be something as simple as missing quotes.
Expand|Select|Wrap|Line Numbers
  1. >>> print hello world
  2. Traceback (  File "<interactive input>", line 1
  3.     print hello world
  4.                     ^
  5. SyntaxError: invalid syntax
  6. >>> print "hello world"
  7. hello world
  8. >>> 
Feb 12 '12 #3

P: 15
D:\Python23>python 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.
Feb 12 '12 #4

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

print "hello world"
Feb 12 '12 #5

P: 15
i still get the same error :-(
Feb 12 '12 #6

bvdet
Expert Mod 2.5K+
P: 2,851
Please post the code you are using.
Feb 12 '12 #7

P: 15
>>> h="hello world"
>>> print h
hello world
Feb 12 '12 #8

bvdet
Expert Mod 2.5K+
P: 2,851
Remove the ">" characters and leading spaces. Your file should contain only:
Expand|Select|Wrap|Line Numbers
  1. h="hello world"
  2. print h
Feb 12 '12 #9

P: 15
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..

Expand|Select|Wrap|Line Numbers
  1. #Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
  2. #Type "copyright", "credits" or "license()" for more information.
  3. #import socket module
  4. from socket import *    
  5. serverSocket = socket(AF_INET, SOCK_STREAM)
  6. #Prepare a sever socket
  7. host=gethostbyname(gethostname())
  8. print host
  9. port=1234
  10. serverSocket.bind((host,port))
  11. while True:
  12.   print 'Ready to serve...'
  13.   serverSocket.listen(1)
  14.   connectionSocket,addr=serverSocket.accept()
  15.   try:
  16.     message = connectionSocket.recv(4096)   
  17.     filename = message.split()[1]  
  18.     f = open(filename[1:])  
  19.         outputdata = f.read()
  20.     #Send one HTTP header line into socket
  21.     #Fill in start
  22.         connectionSocket.send("200 OK") 
  23.     #Fill in end  
  24.     #Send the content of the requested file to the client
  25.         for i in range(0, len(outputdata)):  
  26.            connectionSocket.send(outputdata[i])
  27.         connectionSocket.close()
  28.   except IOError:
  29.      #Send response message for file not found
  30.      #Fill in start
  31.      connectionSocket.send("404 file not found")
  32.      #Fill in end
  33.      #Close client socket
  34.      #Fill in start
  35.      connectionSocket.close()
  36.      #Fill in end  
  37. serverSocket.close()
Feb 12 '12 #10

P: 15
when i type in the python GUI i am unable to remove the >>>

may b am troublin u a lot.. :-D
Feb 12 '12 #11

bvdet
Expert Mod 2.5K+
P: 2,851
">>> " 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.
Feb 12 '12 #12

P: 15
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.
Expand|Select|Wrap|Line Numbers
  1. from socket import *
  2. s = socket(AF_INET,SOCK_STREAM) 
  3. h=gethostbyname(gethostname())
  4. print'host is ',h
  5. s.bind((h,9000))
  6. s.listen(1)
  7. while 1:
  8.     try:
  9.     client, address = s.accept()
  10.     print'connection from',address
  11.     data = client.recv(1024)
  12.     if not data:break 
  13.         client.send(data) 
  14.     client.close()
  15.  
  16.     except IOError:
  17.         print'IOError is there !'
  18.         client.close()
i get this following error :

D:\Python23>python serv.py
File "serv.py", line 10
print"connection from",address
^
SyntaxError: invalid syntax
Feb 12 '12 #13

bvdet
Expert Mod 2.5K+
P: 2,851
Please use code tags when posting code. See "Need help with Bytes? (FAQ)"
Feb 12 '12 #14

bvdet
Expert Mod 2.5K+
P: 2,851
Your code is not indented properly. The interpreter expects an indentation after a try statement.
Feb 12 '12 #15

P: 15
oh sorry.. i am new to the forum.. ll use code tag :)
Feb 12 '12 #16

P: 15
i tried using indentation.. but the error comes in the print line following the client accept code. i.e 2nd line from try. :(
Feb 12 '12 #17

bvdet
Expert Mod 2.5K+
P: 2,851
No problem. Code formatting does not display properly unless code tags are used. You can easily add them by selecting the text you want to wrap in tags and picking the "#" button at the top of the reply widget.
Feb 12 '12 #18

P: 15
i rectified the error myself :) thanku sooo much.. :)
Feb 12 '12 #19

P: 15
the error was all about indentation and proper port mentioning :-)
Feb 12 '12 #20

bvdet
Expert Mod 2.5K+
P: 2,851
I suspect it is still an indentation problem. Your indentation should look like this:
Expand|Select|Wrap|Line Numbers
  1. from socket import *
  2. s = socket(AF_INET,SOCK_STREAM) 
  3. h=gethostbyname(gethostname())
  4. print 'host is ',h
  5. s.bind((h,9000))
  6. s.listen(1)
  7. while 1:
  8.     try:
  9.         client, address = s.accept()
  10.         print 'connection from', address
  11.         data = client.recv(1024)
  12.         if not data:
  13.             break 
  14.         client.send(data) 
  15.         client.close()
  16.  
  17.     except IOError:
  18.         print'IOError is there !'
  19.         client.close()
Note that the number of spaces representing an indentation is a matter of preference but should be consistent. I prefer 4 spaces. Also note it is bad practice to mix spaces and tabs.
Feb 12 '12 #21

P: 15
oh okays :) got it :)
Feb 12 '12 #22

Post your reply

Sign in to post your reply or Sign up for a free account.