I'm new to programming. I''ve successfully wrote everybody's first program , however , it executes right before closing itself. How can I add a pause to the program?
3 Answers
You need to put a call to input
at the bottom of your script:
print("Hello World!")
input("Press 'Enter' to exit.") # Use raw_input in Python 2.x
Doing so will keep the output window open until you press Enter.
Modify the code to be like this, you will learn about the below function soon enough:
print('Hello world!')
input()
Now at the end of the program it won't exit until you press Return.
Or better still, run the code in a shell like IDLE so you don't have to worry about all that.

- 19,907
- 5
- 54
- 57
If you literally want to add a fixed pause, use the sleep
function in the time
module. The is discussed in detail in the answer to How can I make a time delay in Python? . Your program then becomes:
import time
print("Hello world")
time.sleep(1)
The first line tells python that time
means the module time
, while the last line says to wait for 1 second. (If you wanted to wait for 2.5 seconds, then change the last line to time.sleep(2.5)
, etc...)

- 1
- 1

- 2,307
- 1
- 13
- 24
-
-
@Harmone - `time.sleep` will wait for as long as you tell it, then the window will close. `input` will keep the window open until you press a key. – Apr 06 '14 at 16:52