-1

Somebody recommended Python to me. Even this has documentation that is hard to fathom and assures me that various features work just like those other programming languages that I also don't know.

Anyway, I write a file with Notepad and it says simply

print ("test")

and I saved it with a .py extension. Luckily my Windows machine knew that means Python and not the country code, top-level domain for Paraguay.

However, a command prompt window appeared showing the word test and then disappeared.

Next step: How do I make it not disappear?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280

3 Answers3

2

Try running the script from the command prompt:

>python test.py

The results should print rather than a window coming up for .1 seconds and disappearing.

nihiser
  • 604
  • 1
  • 15
  • 28
2

This happens because the program finishes running and exits.

If you run the script from the command line, it will still finish but will return back to the console, instead of closing the window.

Failing that, you can force the script to wait for user input before continuing...

input("Press Enter to close...")

Finally, you can use IDLE:

IDLE is a simple program editor that comes installed with Python. Among other features it can run your programs in a window. Right-click on your .py file and choose "Edit in IDLE". When your program appears in the editor, press F5 or choose "Run module" from the "Run" menu. Your program will run in a window that stays open after your program ends, and in which you can enter Python commands to run immediately.

Basic
  • 26,321
  • 24
  • 115
  • 201
1

You can add a line like

input("Please press return...");

at the end of the program. This will interrupt program execution until the return key is pressed (like the DOS pause command, but not restricted to DOS/Windows).

EDIT: Changed raw_input to input (there is no raw_input in Python 3.x)

Abrixas2
  • 3,207
  • 1
  • 20
  • 22
  • 1
    raw_input is only valid for Python 2.x : http://stackoverflow.com/a/1135035/156755 – Basic Sep 18 '13 at 14:41
  • In 2.7, if I press Enter in response to an `input`, I get `SyntaxError: unexpected EOF while parsing`. This is a rather tricky compatibility problem :-) – Kevin Sep 18 '13 at 14:48