使用.pop()函数获取用户输入以替换lis中的某个点时出错

2024-06-06 07:03:49 发布

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

给出TypeError且不确定原因,希望输入的索引删除列表中的一个点:

      x = input('Which part of the list would you like to remove?(index starts at 0):')

      myThing.pop()[x]

      print(myThing)

Tags: ofthetoyouwhich列表input原因
1条回答
网友
1楼 · 发布于 2024-06-06 07:03:49
x = input('Which part of the list would you like to remove?(index starts at 0):')

myThing.pop(int(x))
print(myThing)

pop将要删除的索引作为参数,因此可以在.pop后面的括号中传递它。此外,还需要将输入转换为int,因为input()返回str

相关问题 更多 >