-2

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?

Harmone
  • 35
  • 4

3 Answers3

0

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.

0

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.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0

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

Community
  • 1
  • 1
ramcdougal
  • 2,307
  • 1
  • 13
  • 24
  • I suppose sleep function is more complex than input function? – Harmone Apr 06 '14 at 16:45
  • @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