问题
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.
回答1:
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.
回答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()
回答3:
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.
来源:https://stackoverflow.com/questions/25005943/window-closes-immediately-after-running-program