I feel like an idiot asking this but is doing quit()
the best way to terminate a python programme? Or is there a better way that could gradually stop all while True loops ect instead of just instantly stopping it all? Once again, I feel like an idiot asking this but I'm just curious.

- 77
- 1
- 2
- 13
-
Do you have created any resources/opened files, etc.? – Jongware Apr 21 '18 at 23:22
-
In this case, it's reading a text file. – Charlie Apr 21 '18 at 23:23
-
1@Charlie what do you mean by "gradually stop all while True loops". Please elaborate. – Apr 21 '18 at 23:27
-
@BOi I don't know, I'm 13 and still an idiot when it comes to coding. I'm just trying to learn as much as possible. – Charlie Apr 21 '18 at 23:30
-
posible duplicate of [Is there a method that tells my program to quit?](https://stackoverflow.com/questions/2823472/is-there-a-method-that-tells-my-program-to-quit) – eyllanesc Apr 21 '18 at 23:30
-
2well @Charlie welcome to coding. You're not an idiot if you're trying to learn. I just wanted you to clarify your question so I can help you with exactly what you want.. – Apr 21 '18 at 23:31
-
@BOi Thanks, and I don't really know what I meant. I just didn't know if it's possible that if you terminate a while True loop if it could possibly result in some unwanted behaviour such as maybe it stopped reading in the middle of a string thus making text have printed or something like that. I really don't know. – Charlie Apr 21 '18 at 23:35
-
@Charlie nothing of that sort happens. When quit() is placed somewhere in your code, the program will just stop no matter what is happening. It won't do anything else. – Apr 21 '18 at 23:43
-
@Charlie if my answer helped please press the check mark next to it to accept it. – Apr 21 '18 at 23:55
-
@BOi I would but I cannot find a check mark. Could it possibly be because I have only 6 reputation because that has stopped my doing some other things? – Charlie Apr 22 '18 at 00:00
-
@Charlie https://i.stack.imgur.com/LkiIZ.png try this. – Apr 22 '18 at 00:04
-
Oh okay sorry, I was looking else-where – Charlie Apr 22 '18 at 00:07
-
@Charlie that is absolutely no problem. Thanks a lot and good luck with whatever you are trying to make. – Apr 22 '18 at 00:07
4 Answers
I don't know why you don't want to use quit()
but you can use this:
import sys
sys.exit()
Or this:
raise SystemExit(0)
To halt a while loop you can use the break
statement. For example:
while True:
if True:
do something #pseudocode
else:
break
The break
statement will immediately halt the while
loop as soon as the else
statement is read by python
You can use the break
statement to stop a while loop. Eg:
while True:
if True:
<do something>
else:
break

- 11,652
- 2
- 48
- 54
Generally, the best way to end a Python program is just to let the code run to completion. For example, a script that looks for "hello" in a file could look like this:
# import whatever other modules you want to use
import some_module
# define functions you will use
def check_file(filename, phrase):
with open filename as f:
while True:
# using a while loop, but you might prefer a for loop
line = f.readline()
if not f:
# got to end of file without finding anything
found = False
break
elif phrase in line:
found = True
break
# note: the break commands will exit the loop, then the function will return
return found
# define the code to run if you call this script on its own
# rather than importing it as a module
if __name__ == '__main__':
if check_file("myfile.txt", "hello"):
print("found 'hello' in myfile.txt")
else:
print("'hello' is not in myfile.txt")
# there's no more code to run here, so the script will end
# -- no need to call quit() or sys.exit()
Note that once the phrase is found or the search comes to the end of the file, the code will break out of the loop, and then the rest of the script will run. Eventually, the script will run out of code to run, and Python will exit or return to the interactive command line.

- 17,670
- 5
- 28
- 45
-
I believe that the OP wants to terminate the program using his commands. I'm pretty sure he knows that letting a program run to completion will end the program soon. – Apr 21 '18 at 23:42
-
1Thank you for going out of your way to post this but I already knew that allowing the programme to run to completion will eventually make it stop. But again thanks. – Charlie Apr 21 '18 at 23:49
-
At the top where it says `def check_file(filename, phrase):` what does "phrase" mean? – Charlie Apr 22 '18 at 00:09
-
@Charlie, the "phrase" variable just holds the text that you're searching for in the file. It could just as well be called "word" or "text." By the way, if you must exit directly from the deep inside the program, the best option is probably to raise an exception (possibly SystemExit). This won't do any harm if you don't have any important unsaved data or unfinished work. It will just end your program (and the computer won't mind if it's in the middle of a loop). – Matthias Fripp Apr 22 '18 at 05:32
-
1@BOi the point I was making is that you almost always can (and should) arrange your code so it runs to the end under normal circumstances. So if the question is, "what's the best way to exit a Python program even if it's in a loop?" then the answer is "setup the logic so your program ends when the job is done." – Matthias Fripp Apr 22 '18 at 05:37
If you want to stop a while True
loop, you could set a variable to True and False, you could even work with a counter if you loop has to stop after a specific amount of loops.
for example
x = 0
y = True
while y == True:
<do something>
x = x + 1
if x == 9:
y = False
just a quick example of what you could do, without using a while loop(basically what i wrote above, but then in 1 line.)
x = 10
for i in range(x):
<do something>
To stop a program, I normally use exit()
or break
.
I hope this somehow helped you, if not; please comment and I'll try helping you!

- 384
- 1
- 12
-
Hey, I don't really understand the second section of code that you sent. `x = 10 for i in range(x):
` Where does the letter `i` come from, or is that just a place holder? – Charlie Apr 21 '18 at 23:53 -
`i` is the variable that's being created, it is indeed a placeholder, you could also write `for charlies in range(x):`. Try it out by typing `for charlies in range(x): print(charlies)`. It will output a counter from 0 till 9 (python starts counting at 0 instead of 1) – DevOps Apr 21 '18 at 23:56
-
1exactly what @DevOps said. It creates a variable called i that can be used to iterate through the range of x. – Apr 21 '18 at 23:58
-