2

I am new to all programming and I just started to get interested in learning how to program. So to do so I started with what most people consider the easiest language: Python.

The problem I am having right now though is that if I say to Python print("Hello!"), save it in a file, and then run it, a black window opens up and closes right away. I just do not understand why it is doing this.

user3885968
  • 31
  • 1
  • 3
  • where are you running it from? – Padraic Cunningham Jul 28 '14 at 23:30
  • form python. I just close it and save it lets say on my desktop and then double click on it. – user3885968 Jul 28 '14 at 23:31
  • 1
    That (mostly) black window probably shows `Hello!` in the top left corner just before it goes away. – Greg Hewgill Jul 28 '14 at 23:31
  • 1
    add `input()` to your code and try again – Padraic Cunningham Jul 28 '14 at 23:32
  • 1
    I assume you're on Windows? When you double-click a `.py` file, by default, it opens a new `cmd` window, runs the script, then closes the window. You can add a `input('Press Return to quit') at the end of your script, but it's better to learn how to either use the command prompt to run scripts, or use an IDE (IDLE, PyCharm, Eclipse/PyDev, etc.), or ideally both. – abarnert Jul 28 '14 at 23:32
  • When the program finishes, Windows (the OS) closes python's window immediately. Either run from a cmd window, or try adding a wait for a keypress to your program (see [this question](http://stackoverflow.com/questions/983354/how-do-i-make-python-to-wait-for-a-pressed-key)) – Blorgbeard Jul 28 '14 at 23:33
  • I would strongly recommend dual booting your Windows machine with a Linux install as it will make programming in Python worlds easier. Most tutorials or books will also assume you are on Linux or Mac. – kylieCatt Jul 28 '14 at 23:42
  • @IanAuld: The differences don't really come up except right at the very beginning (which he's almost past) and a lot further in. Also, you can just use Cygwin to get a nice POSIX-y environment inside Windows; alternatively, use a virtual machine; no need to dual-boot. – abarnert Jul 28 '14 at 23:54
  • 2
    Upvoted because I don't know why this was downvoted. (Sure, the poster is clearly a novice, but it's not like he didn't admit that right up front. He describes the problem pretty clearly, and specifically; maybe it doesn't belong on SO, but then we should migrate or close it, not leave it here and downvote it.) – abarnert Jul 28 '14 at 23:58
  • @abarnert I have worked with someone who used a VM before and he ran into problems almost everywhere. It's much simpler to install Ubuntu with Wubi and run a dual boot than to deal with the hassles of developing on Windows, at least in my opinion. Also most job listings in the field will expect you to know how to navigate on a Linux box. – kylieCatt Jul 28 '14 at 23:59
  • 2
    @IanAuld: I can't remember any significant problems running linux under a VM since the 1.x kernel days. In fact, I've had far _more_ problems running it on bare metal… Also, running it in a VM doesn't affect navigating in any way; the virtual machine _is_ a linux box. Also, what makes you think the OP is hoping to get a job as a professional developer in today's job market? For that matter, you really think all the people who work on iOS or Windows or other platforms are expected to know how to navigate on a linux box? – abarnert Jul 29 '14 at 00:56

3 Answers3

4

It is normal for the window to close as soon as your program runs to completion. If you want it to stay open, you can add a call to input (or raw_input if you are using Python 2.x) at the end:

print("Hello!")
input("Press the <Enter> key on the keyboard to exit.")

This will keep the window open until you press the Enter key on the keyboard.

  • The only thing that can be clicked on to close the window is its OS close button in the upper right -- there's no "Enter" button displayed. – martineau Jul 29 '14 at 00:52
  • @martineau - I thought it was clear that I meant the Enter key on the keyboard. Sorry, I'll edit my answer to say that. –  Jul 29 '14 at 00:59
  • @iCodez: But I can't get the mouse cursor to move off the screen and onto my keyboard. :P – abarnert Jul 29 '14 at 01:18
  • 1
    Ugh. I am starting to regret answering this. -_- –  Jul 29 '14 at 01:22
2

Well because a print command simply writes something to the terminal.

In order to see the output. Start cmd (or open a terminal in Linux) and then run:

python <file>

with <file> the file you want to run...

Or you can, like @iCodez suggests, pause the input at the end of the program by adding a pause:

print("Hello World!")
input()
abarnert
  • 354,177
  • 51
  • 601
  • 671
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • oh like on powershell? – user3885968 Jul 28 '14 at 23:32
  • Yes... Indeed. Another option is to pause the program until you've read the output. – Willem Van Onsem Jul 28 '14 at 23:33
  • 1
    @user3885968: Yes, you can use PowerShell, although cmd (aka "DOS prompt") can be easier to use once you get further along, because it fits in better with the Unix-like model Python was designed around (things like standard input, argv, etc. make more sense in cmd). – abarnert Jul 28 '14 at 23:33
  • @abarnert thanks though I hope i wont annoy you but from cmd, how do I start the program that just print("Hello!")? – user3885968 Jul 28 '14 at 23:40
  • 1
    @user3885968: The same way the answer shows. Assuming `python.exe` is somewhere on your path, and you've changed directory to the location of your script, `python myscript.py` will run it. If `python.exe` isn't on your path you'll have to do `C:\Python34\bin\python.exe myscript.py`; if the script isn't in your current directory, `python D:\myscripts\myscript.py`; or of course the two of those combined if necessary. – abarnert Jul 28 '14 at 23:56
1

Insert input() in the last line. It will make the program wait for a input. While it doesn't occur the windows program will be open. If you press any key and then enter, it will close.

majno
  • 9
  • 2