-2

A real noob question.

I just wrote my first Hello World ! python program

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def main():
    print ('Hello World !')
    return 0

if __name__ == '__main__':
    main()

Unfortunately, when I launch it, it opens the terminal for a fraction of a second. How to avoid that it exits straight.

I look for something like pause in Windows command line.

Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
  • No offense but a very fast search on google gave me this, took me 8 seconds - `raw_input('Press Enter to exit')` – Dayan Feb 25 '14 at 19:37

2 Answers2

5

Wait for a keystroke:

raw_input("Press <Enter> to exit.")

This will prompt the user for input and wait until it is received, at which point, the program will exit

That1Guy
  • 7,075
  • 4
  • 47
  • 59
3

There's several solutions:

  • Launch a command line and then run your Python program in it, with python <your_file>, it'll not exit the console ;

  • With a good IDE, you can put a breakpoint at the end of your main (example with Pydev) ;

  • Ask a fake user input, which will freeze the program, waiting for the input

    if __name__ == '__main__':
         main()
         input("Press <Enter> to exit.")      # Python 3, raw_input if Py2
    
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97