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

女爷i 2022-08-24 00:51 293阅读 0赞

举例说明:

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

结果:

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

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

解决方法:加上else语句

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

结果:

  1. >>>
  2. False 4

参考文章:

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

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读