首页 > 解决方案 > 元组概念:TypeError:“NoneType”对象不可迭代

问题描述

代码 :

input_tuple = ('Monty Python', 'British', 1969)
y = list(input_tuple)
z = y.append("Python")
tuple_2 = tuple(z)
print(tuple_2)

预期 o/p

('Monty Python', 'British', 1969, 'Python')

但得到

TypeError                                 Traceback (most recent call last)
<ipython-input-30-037856922f23> in <module>
      3 y = list(input_tuple)
      4 z = y.append("Python")
----> 5 tuple_2 = tuple(z)
      6 # Make sure to name the final tuple 'tuple_2'
      7 print(tuple_2)

TypeError: 'NoneType' object is not iterable

标签: python

解决方案


这不是 append 的工作方式。您不需要在 z 中保存 y.append 值,它将直接在 y 中更新,因此创建修改后 y 的元组

看看这个,这对你有用..

input_tuple = ('Monty Python', 'British', 1969)
y = list(input_tuple)
y.append("Python")
tuple_2 = tuple(y)
print(tuple_2)

推荐阅读