新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中assert斷言語句怎么用?
Assert斷言語句經常是用于確認現有的兩個內容是不是滿足條件的,若不滿足,斷言語句引發(fā)異常,大家有沒有切身體驗過這種異常報錯呢?沒有的話,下面給大家演示下,正常報錯顯示的現象以及處理辦法,遇到問題的小伙伴可以跟著來了解下啦~

直接用一個例子演示:
def strToInt10(x):
assert isinstance(x, str), 'x must be string'
assert x.isdigit(), 'x must be digit'
return int(x)
print(strToInt10('10'))
strToInt10([1])
結果異常:
10 ---- AssertionError Traceback (most recent callin strToInt10(x) 1 def strToInt10(x): ----> 2 assert isinstance(x, str), 'x must be string' 3 assert x.isdigit(), 'x must be digit' 4 return int(x) 5 print(strToInt10('10')) AssertionError: x must be string
雖然上報代碼顯示報錯,但是我們可以使用try處理這些異常的,大家如果需要調用這個語句,可以依照上述小編給大家演示的效果進行實踐哦~
當前題目:創(chuàng)新互聯(lián)Python教程:python中assert斷言語句怎么用?
當前地址:http://www.5511xx.com/article/dhdjcpj.html


咨詢
建站咨詢
