Best way to quit a python programme?

旧城冷巷雨未停 提交于 2021-01-25 08:18:33

问题


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.


回答1:


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




回答2:


You can use the break statement to stop a while loop. Eg:

while True:
    if True:
        <do something>
    else:
        break



回答3:


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.




回答4:


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!



来源:https://stackoverflow.com/questions/49961051/best-way-to-quit-a-python-programme

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