需要帮助了解我为什么得到attribu吗

2024-05-16 23:21:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚从Eric Matthes的python速成班开始学习python,它一直告诉我我有一个属性错误

motor_bikes = 'harley', 'Fz07', 'Crouch rocket'

last_owned = motor_bikes.pop()

print(last_owned)

假设从列表中弹出最后一个索引,以便在新变量lasted_owned中使用它


Tags: 属性错误poplastprintericrocketmotor
1条回答
网友
1楼 · 发布于 2024-05-16 23:21:20

您正在创建一个tuple,而不是一个list。元组是不可变的,它们没有.pop()。这个错误会告诉您:

AttributeError: 'tuple' object has no attribute 'pop'
motor_bikes = ['harley', 'Fz07', 'Crouch rocket']  # this is a muteable list

last_owned = motor_bikes.pop()  # it has a pop method

print(last_owned)

作品:

Crouch rocket

您可以通过以下方式创建元组:

t1 = 1,3,4
t2 = 1,
t3 = (1,2,3,)

print(t1,t2,t3)  # (1, 3, 4), (1,), (1, 2, 3)  -all tuples

相关问题 更多 >