Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

How to stop Python closing immediately when executed in Microsoft Windows

I have just started college and we are going to be using python. We really have done nothing so I have downloaded the program and done some print commands, and that's it.

When I run my .py file (a print command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.

I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.

Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried input("prompt: ") as suggested by someone, and that made no difference.

If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?

question from:https://stackoverflow.com/questions/12375173/how-to-stop-python-closing-immediately-when-executed-in-microsoft-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Python 3, add the following to the end of your code:

input('Press ENTER to exit')

This will cause the program to wait for user input, with pressing ENTER causing the program to finish.

You can double click on your script.py file in Windows conveniently this way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...