天天看点

Python出现TypeError: 'NoneType' object is not iterable

举例说明:

def contain():
        score = 4    
        if score in num:                                          
            return True,score;
        
num = [1,2,3,0]
iscontain,score = contain()  
print iscontain,score
           

结果:

>>> 

Traceback (most recent call last):
  File "D:\Program Files\python\chengxu\temp.py", line 8, in <module>
    iscontain,score = contain()
TypeError: 'NoneType' object is not iterable</span>
>>> 
           

说明:当只有if条件并返回多个变量时,如果if条件不满足会出现异常

解决方法:加上else语句

def contain():
        score = 4    
        if score in num:                                          
            return True,score;
        else:
            return False,score;
num = [1,2,3,0]
iscontain,score = contain()  
print iscontain,score
           

结果:

>>> 
False 4
           

参考文章:

http://blog.csdn.net/dataspark/article/details/9953225