国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python異常中else的實(shí)例用法

瀏覽:5日期:2022-06-16 18:22:28
1、說明

當(dāng)確定沒有異常后,還需要做一些事情可以使用else語句。

注意:try中沒有異常,else之后的代碼才會被執(zhí)行。

2、實(shí)例

while True: try:x = int(input(’請輸入X:’))y = int(input(’請輸入Y:’))value = x / yprint(’x/y is’,value) except Exception as e: # 發(fā)生異常時(shí)執(zhí)行print(’不正確的輸入:’, e)print(’請重新輸入’) else: # 未發(fā)生異常時(shí)執(zhí)行break

實(shí)例擴(kuò)展:

def fetcher(obj, index): return obj[index] x = ’spam’ try: print fetcher(x, 3)except Exception: print ’hhh’else: print ’has no exception’ print fetcher(x, 2) print ’---’ * 10 try: print fetcher(x, 4)except IndexError: print ’got exception’else: print ’has no exception’ print fetcher(x, 2)

運(yùn)行結(jié)果:

mhas no exceptiona------------------------------got exception

到此這篇關(guān)于python異常中else的實(shí)例用法的文章就介紹到這了,更多相關(guān)python異常中else的使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长沙市| 华亭县| 绥中县| 铜山县| 苍梧县| 德州市| 镶黄旗| 东乡族自治县| 沈阳市| 耒阳市| 武隆县| 安新县| 泰顺县| 宁阳县| 苍溪县| 新化县| 安陆市| 信阳市| 绥阳县| 团风县| 泾川县| 宁波市| 简阳市| 谢通门县| 娄烦县| 全州县| 平塘县| 凭祥市| 华蓥市| 大埔县| 公安县| 清丰县| 赣州市| 岫岩| 榆树市| 平罗县| 名山县| 瑞昌市| 长海县| 岗巴县| 疏勒县|