一段代碼(截?。篺or anyNumber in lineBack:if anyNumber.isdigit() == False:anyNumber = int(anyNumber)print anyNumber,if anyNumber > 30000:anyNumber -= 30000為啥會報出錯誤:TypeError: unsupported operand type(s) for -=: 'str' and 'int'我已經(jīng)將str型轉(zhuǎn)成了int型了啊請指教空格和縮進(jìn)在代碼中都沒有問題,貼到百度中可能對齊不好。主要是報的錯事TypeError: unsupported operand type(s) for -=: 'str' and 'int',我的 lineBack中全是數(shù)字(不論是什么型的),而我已經(jīng)將字符型的數(shù)字轉(zhuǎn)成數(shù)字型了。請指教
2 回答

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗 獲得超4個贊
對于類似“123”這樣的字符串,anyNumber.isdigit()返回值為True,程序中并沒有對這樣的字符串進(jìn)行轉(zhuǎn)換。
你可能理解錯了isdigit()函數(shù)的功能。isdigit()函數(shù)是功能“Return True if all characters in S are digits and there is at least one character in S, False otherwise.” 換句話說,用于判斷一個字符串“只”包含數(shù)字字符。

慕標(biāo)5832272
TA貢獻(xiàn)1966條經(jīng)驗 獲得超4個贊
如果滿足以一個if語句的話確實轉(zhuǎn)為int了
如果沒滿足的話就會判斷anyNumber是不是大于30000了,注意空格阿!
添加回答
舉報
0/150
提交
取消