简体   繁体   中英

Window closes immediately after running program

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM