How to terminate a Python script

前端 未结 10 1060
予麋鹿
予麋鹿 2020-11-22 04:34

I am aware of the die() command in PHP which exits a script early.

How can I do this in Python?

10条回答
  •  隐瞒了意图╮
    2020-11-22 04:45

    In Python 3.5, I tried to incorporate similar code without use of modules (e.g. sys, Biopy) other than what's built-in to stop the script and print an error message to my users. Here's my example:

    ## My example:
    if "ATG" in my_DNA: 
        ## 
    else: 
        print("Start codon is missing! Check your DNA sequence!")
        exit() ## as most folks said above
    

    Later on, I found it is more succinct to just throw an error:

    ## My example revised:
    if "ATG" in my_DNA: 
        ## 
    else: 
        raise ValueError("Start codon is missing! Check your DNA sequence!")
    

提交回复
热议问题