I wrote the python “Hello World program using the Sublime editor. I built it and saved it. When I run it, the terminal screen flashes on and then disappears. How can I get it to remain on screen as it is supposed to according to the “Python Crash Course” book?
Tried from the text editor and by running the file from Windows Explorer. Same result each time.
Michael E Wilmer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If you don’t already have your terminal open, then the terminal only stays open as long as the program is running. A “hello world” program doesn’t take very long to run.
Two solutions:
- Open your terminal and run the program from there. Since it was already open it won’t close when your program completes.
- Add a line to your program asking for user input. You don’t have to do anything with it, but waiting for the input will keep the terminal open.
E.g.
print('hello world!')
input('press any key to finish')
Once all of the code is done running, the terminal closes. So what happens is it runs print("Hello, World!")
and then closes the terminal, since all of the code has ran.
What you can do is either what some above answers have said:
print("Hello, World!")
input("Enter an input to close.")
Since python waits for your input, it will wait until you type something and press enter to close.
Here’s another way:
print("Hello, World!")
while True:
pass
The while
loop waits forever, so it will only close when you close the terminal.
And finally, you can just get an IDE like pycharm or VSCode
Codeirie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
maybe you can add input()
at the end of your code, then your program will wait until you type.
Or, you can use
if sys.argv[- 1] == "no_close:
input(“enter any key to continue...")
Or you could use pycharm, an IDE for python specifically.
Saury is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.