導航:首頁 > 編程語言 > pythonchecktype

pythonchecktype

發布時間:2022-05-15 23:13:51

① 在python中 關聯兩個文件的錯誤信息 返回實際的值的問題

# tips 在命名中,1和l太像,容易混淆 我命名中去除了1

#另外 你那句int(x)==False 不知道你想表達什麼意思 我修改為temp=int(x)

#另外 對於Ha的raiser函數,不知道你想表達啥意思,如果輸入了字元串『123』你是直接raise錯誤呢

#還是你想表達如果輸入的是數字,而不是字元串,我暫且認為你是當輸入特定的『123』字元串

#haa.py
classHaError(Exception):
pass
classHa:
def__init__(self,x):
self.raiser(x)
defraiser(self,x):
ifx=='123':
raiseHaError('shouldbestr')
else:
temp=int(x)

#hbb.py

fromhaaimportHaError
fromhaaimportHa
deftext(x):
try:
Ha(x)
exceptValueError:
return'Value'
exceptHaError:
return'String'
else:
return'OK'

#然後是運行結果

Python3.4.0b2(v3.4.0b2:ba32913eb13e,Jan52014,16:13:26)[MSCv.160064bit(AMD64)]onwin32
Type"right","credits"or"license()"formoreinformation.
>>>================================RESTART================================
>>>
>>>text('e')
'Value'
>>>text('123')
'String'
>>>text('456')
'OK'
>>>text(456)
'OK'
>>>

#最後,大膽的猜測,樓主是否想表達,輸入的東西比如是只包含數字的字元串,其他輸入數字

#或者別的都不行呢 如果是這樣 我覺得不需要這么麻煩,直接用 type(x)函數 和 isdigit() 判斷

# 只用一個函數就ok了 如下:

#python3.4

deftext(x):
temp={0:'Value',1:'Str',2:'OK',3:'None'}
defcheckType():
iftype(x)in(float,int):
return0
eliftype(x)==str:
ifx.isdigit():
return2
else:
return1
else:
return3
returntemp[checkType()]
閱讀全文

與pythonchecktype相關的資料

熱點內容
java聊天窗口 瀏覽:976
單片機控制陣列led燈 瀏覽:577
白鹿用的什麼APP修圖 瀏覽:499
阿里雲輕量應用伺服器ssh無法連接 瀏覽:794
員工福利系統源碼 瀏覽:982
數據加密如何設置 瀏覽:570
php取余運算 瀏覽:153
php如何壓縮圖片大小 瀏覽:137
編程三階教程 瀏覽:983
pdf顏色查看 瀏覽:469
怎麼用指令停用命令方塊java 瀏覽:406
滑鼠命令行 瀏覽:567
如何朗讀pdf 瀏覽:746
壓縮機啟動後繼電器發燙 瀏覽:405
小學編程項目學習 瀏覽:557
net編譯運行原理 瀏覽:786
加密電腦的文件拷出來打不開 瀏覽:366
可達性演算法根 瀏覽:208
ibm的伺服器怎麼安裝系統 瀏覽:492
pdftomobi在線 瀏覽:797