PDA

View Full Version : [SOLVED] Python newbie, help needed



hyperAura
May 1st, 2011, 10:04 AM
Hi guys,

I decided to start programming in Python as I have a project in my mind and I think that Python is the right language for it.

For programming I personally use IDE's, so I installed IDLE 3 for Python.

My problem is that when I run in IDLE the following command


>>>print "hello world"I get syntax error, whereas if I run the same command in a terminal window, it runs ok..

Does anyone know what the problem might be?? Or maybe a better editor if it exists?

Thanks

dv3500ea
May 1st, 2011, 11:08 AM
In python 3 you need to have brackets around what you are printing because print is now a function.

Use:


print("hello world")

instead.

hyperAura
May 1st, 2011, 01:30 PM
ok now I am trying to use the sys.exit() function..

On top of my code I did:

import sys

...
Code
sys.exit()
...

The program exits but I also get the following message which I don't know if it is the normal behavior:



Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
parseAddress("://")
File "/home/hyperaura/Python/webparse.py", line 7, in parseAddress
sys.exit()
SystemExit


The text is in red color so I guess that I might not be using the exit function properly..
Also the sys.exit() is not highlighted whereas all the other functions are written in purple..

thanks

hakermania
May 1st, 2011, 02:22 PM
I know almost nothing about python, but maybe ou should provide an exit status/number?
like
sys.exit(0)

hyperAura
May 1st, 2011, 04:33 PM
hey, I have tried your suggestion but the output remains the same..

cgroza
May 1st, 2011, 05:46 PM
hey, I have tried your suggestion but the output remains the same..
Open a terminal, start python3, and try the function call. If it is okay, it is an IDLE thing.

simeon87
May 1st, 2011, 06:22 PM
You are using sys.exit correctly: http://docs.python.org/library/sys.html#sys.exit However, sys.exit doesn't exit the system but it just raises a SystemExit exception (as you've seen). Normally, this can exit the system but it may be that IDLE shows the exception instead. This is also in the docs:


Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.

It's likely that execution is handed over to IDLE when you raise SystemExit.

cgroza
May 1st, 2011, 07:33 PM
You are using sys.exit correctly: http://docs.python.org/library/sys.html#sys.exit However, sys.exit doesn't exit the system but it just raises a SystemExit exception (as you've seen). Normally, this can exit the system but it may be that IDLE shows the exception instead. This is also in the docs:



It's likely that execution is handed over to IDLE when you raise SystemExit.
So this code will stop application exit?




import sys

try:
sys.exit(0)
except SystemExit:
#do something else?

hyperAura
May 1st, 2011, 11:14 PM
hey cgroza I am guessing that the interrupt is not written like this but instead it is going to be an interrupt from a peripheral such as the keyboard or a mouse click..

I believe that the code you posted will just exit, without performing what is after the sys.exit() line..

nvteighen
May 2nd, 2011, 07:20 AM
sys.exit means 'interrupt the thread regardless of what stack frame we are'. In a single-threaded program, that will interrupt the whole program. And it takes an integer as argument so you can return a value to whatever called the thread or program.

Different thing is how it is implemented in Python (an exception). You shouldn't care about that: you'll get a SystemExit only when you've directed so (via sys.exit or by manually raising SystemExit), so a priori it makes little sense to catch it.

hyperAura
May 5th, 2011, 06:47 PM
hey thanks a lot.. I was just worried with the traceback which appears in red color, whether my code was correct

andrewgail
May 7th, 2011, 12:34 AM
Yes, Python is great programming language for any type project. Python encourages program reusability by implementing modules and packages. A big set of modules has already been developed and is provided as The Standard Python Library, which is allotment of the Python distribution.