. Advertisement .
..3..
. Advertisement .
..4..
Please help me! I get TypeError: ‘NoneType’ object is not iterable with this code:
countryList =["India","China","Russia","France"]
def getCountryList():
countryList.append("italy")
for country in getCountryList():
print(country)
When I launch the above program, I received the following results:
TypeError Traceback (most recent call last)
<ipython-input-51-1558beedb4d8> in <module>
3 countryList.append("italy")
4
----> 5 for country in getCountryList():
6 print(country)
TypeError: 'NoneType' object is not iterable
Does anyone know why such an error occurs? Please help me fix it.
Thanks.
The cause: If you try to iterate through a
None
object in afor
orwhile
loop, you’ll make the error TypeError: ‘NoneType’ object is not iterable.On your program,
countryList
is of typeNone
, that is the cause of your error. Because you neglected to return listcountryList
fromgetCountryList()
.Solution:
In this situation, we must return list
countryList
fromgetCountryList()
to fix the issue.