I've just started in Python and I'm still feeling my way around, I tried to create a quick program to display "Welcome to Python!" without the quotes.

The entirety of the code is this:

print('Welcome to Python!')

However, when I double-click on the .py file Python opens up for less than a second in which Welcome to Python is visible before immediately exiting.

I've tried putting that same line of code directly into the Command line without using the .py file and it works fine, any help is appreciated!

7
Contributors
7
Replies
9
Views
8 Years
Discussion Span
Last Post by BlueNN

just put raw_input("Press enter to close") at the end of your code and then it will wait until you press enter to close. This is how i got around that problem.

Yeah, as soon as the script is done in the command prompt, it'll exit. So put something like waiting for input at the end so that it pauses the program.

Alternatively, you could always make a batch (.bat ) file to run instead with the lines:

python "path\to\my\file.py"
pause

That'll make the DOS prompt pause after running your script.

or you could use the time module:

from time import sleep
print('Welcome to Python')
sleep(3) # this will pause it for 3 seconds

Or write and run your program using one of the many Python IDEs like IDLE or DrPython that have their own output window.

Just a correction to the first response: raw_input() is now input() for Python 3.0. You can put input() at the very end of your scripts if you are running them without IDLE (although I recommend IDLE for beginners) and it will wait until you press ENTER before exiting.

yes to compile the answers into one post there are three ways that me and my associates can think of:

putting:

#code here
input('Please press enter to close: ')
#raw_input() on any IDLE except 3

#or

import time
#code here
time.sleep(10)
#put the time in sec you want it to sleep before closing...
#or

#file here for command prompt/terminal prompt
pause

and guys could you possibly take your suggestions on what python editing program is better else where... BlueNN can decide for him/her self...

Thanks for all of your help! I eventually decided to use the input option and now I've been able to progress once more on my journey into Python!

Just so you know I'm using IDLE which came with the download. I might get a different one later on but for now I'm just going to stick with it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.