473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

21 6799
Can you share your code here?
Feb 12 '12 #2
bvdet
2,851 Expert Mod 2GB
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
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
try to put quatos after print inside of hii.py:

print "hello world"
Feb 12 '12 #5
i still get the same error :-(
Feb 12 '12 #6
bvdet
2,851 Expert Mod 2GB
Please post the code you are using.
Feb 12 '12 #7
>>> h="hello world"
>>> print h
hello world
Feb 12 '12 #8
bvdet
2,851 Expert Mod 2GB
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
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
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
2,851 Expert Mod 2GB
">>> " 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
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
2,851 Expert Mod 2GB
Please use code tags when posting code. See "Need help with Bytes? (FAQ)"
Feb 12 '12 #14
bvdet
2,851 Expert Mod 2GB
Your code is not indented properly. The interpreter expects an indentation after a try statement.
Feb 12 '12 #15
oh sorry.. i am new to the forum.. ll use code tag :)
Feb 12 '12 #16
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
2,851 Expert Mod 2GB
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
i rectified the error myself :) thanku sooo much.. :)
Feb 12 '12 #19
the error was all about indentation and proper port mentioning :-)
Feb 12 '12 #20
bvdet
2,851 Expert Mod 2GB
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
oh okays :) got it :)
Feb 12 '12 #22

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

Similar topics

7
by: Petr Prikryl | last post by:
Hi, Summary: In my opinion, the C-like prefix increment and decrement operators (++i and --i) should be marked as "syntax error". Current situation: try... (Python 2.4 (#60, ...)) >>> i =...
11
by: frr | last post by:
Hi, After upgrading to 2.4 (from 2.3), I'm getting a weird syntax error: >>> import themes Traceback (most recent call last): File "<interactive input>", line 1, in ? File "themes.py", line...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
2
by: py | last post by:
Hi, I am running python 2.4.2 on win xp pro. I have the WMI module from Tim Golden (http://tgolden.sc.sabren.com/python/wmi.html). I have some code which does this... MyScript.py...
4
by: daniel | last post by:
I'm not quite new to this language, but such error has never happened so frequently before. I have no idea what to do about it, because there's actually no syntax error at all !! I'm using...
8
by: Michael Press | last post by:
I have not written python codes nor run any. I saw this code posted and decided to try it. It fails. I read the tutorial and the entry for the built in function sum, but still do not see the...
7
by: Dustin MacDonald | last post by:
Hi everyone. This is my first time posting to this newsgroup, and although I maintain my netiquette I might've missed something specific to the newsgroup, so hopefully you can avoid flaming me...
2
by: Karthik Gurusamy | last post by:
I see python doesn't have ++ or -- operators unlike say, C. I read some reasonings talking about immutable scalars and using ++/-- doesn't make much sense in python (not sure if ++i is that...
0
by: Alexis Boutillier | last post by:
Hi, I have a strange behaviour of python with pdb and import statement. Here is the example code : file my1.py: import my2 file my2.py: a=5
0
by: Timothy Grant | last post by:
On Fri, Aug 29, 2008 at 12:48 AM, Alexis Boutillier <alexis.boutillier@arteris.comwrote: http://docs.python.org/ref/import.html -- Stand Fast, tjg.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.