元组概念:类型错误:'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

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

最新更新