Window closes immediately after running program

末鹿安然 提交于 2021-02-08 08:25:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!