在Python中是否有任何"除非"工作像"除",但对于普通代码,而不是异常

Mar*_*tin 1 conditional python-3.x

我知道有人问起的"除非",它作为if not,或者not in,但我想有一个引入了一个例外条件语句的语句.例如:

if num >= 0 and num <= 99:
    # then do sth (except the following line)
unless num = 10: 
    # in this case do something else
Run Code Online (Sandbox Code Playgroud)

它比写作更清晰,更直观:

    if (num >= 0 and num <= 9) or (num >= 11 and num <= 99):
        # then do sth (except the following line)
    elif num = 10: 
        # in this case do something else
Run Code Online (Sandbox Code Playgroud)

如果不是声明不同意......

注意:我几乎是新手,所以请耐心等待

小智 7

如果你重新排序这些条款,普通的if可以这样做:

if num == 10:
    # do the num = 10 thing
elif 0 <= num <= 99:
    # do something else
Run Code Online (Sandbox Code Playgroud)