请教关于python的str和int转换

一段代码(截取):
for 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'
我已经将str型转成了int型了啊
请指教
空格和缩进在代码中都没有问题,贴到百度中可能对齐不好。主要是报的错事TypeError: unsupported operand type(s) for -=: 'str' and 'int',我的 lineBack中全是数字(不论是什么型的),而我已经将字符型的数字转成数字型了。请指教

对于类似“123”这样的字符串,anyNumber.isdigit()返回值为True,程序中并没有对这样的字符串进行转换。
你可能理解错了isdigit()函数的功能。isdigit()函数是功能“Return True if all characters in S are digits and there is at least one character in S, False otherwise.” 换句话说,用于判断一个字符串“只”包含数字字符。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-01-09
如果满足以一个if语句的话确实转为int了
如果没满足的话就会判断anyNumber是不是大于30000了,注意空格阿!
第2个回答  2011-01-09
for anyNumber in lineBack:
if anyNumber.isdigit() == False:
anyNumber = int(anyNumber)
print anyNumber,
if anyNumber > 30000:
anyNumber -= 30000
注意缩进
相似回答