美文网首页
python:大白用else

python:大白用else

作者: 大白python | 来源:发表于2019-07-26 16:10 被阅读0次

    对于大白,else一直用在if语句,是少看代码了,原不知与它搭配的地方还真不少,用起来还真好用。

    正文:
    1,For-else结构在搜索某些东西并找到它时很有用

    for i in mylist:
    if i == theflag:
    break
    process(i)
    else:
    raise ValueError("List argument missing terminal flag.")

    2,Try-catch-else结构
    try:
    foo()
    except Exception:
    print("Exception occured")
    else:
    print("Exception didnt occur")
    finally:
    print("Always gets here")

    3,While-else结构
    i = 5

    while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
    break
    else:
    print("Finished up!")

    python:大白用else

    相关文章

      网友评论

          本文标题:python:大白用else

          本文链接:https://www.haomeiwen.com/subject/gnddrctx.html