Tag Archives: NoneType

Typeerror in Python: ‘nonetype’ object is not Iterable

For example:

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

Results:

>>> 

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>
>>> 

Note: When there is only an if condition and multiple variables are returned, an exception will occur if the if condition is not satisfied

Solution: Add an else statement

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

Results:

>>> 
False 4

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