元组概念:TypeError:'NoneType' 对象不可迭代

时间:2021-04-10 13:58:14

标签: python

代码:

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

1 个答案:

答案 0 :(得分:2)

这不是 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)