带有列表的python TypeError

时间:2018-08-24 13:13:20

标签: python string list function typeerror

为什么会出现错误“ TypeError:列表索引必须是整数或切片,而不是str”??但是,如果我将列表中的值更改为整数,是否不会items.remove(items[1])只是删除索引位置1中的项目?

def choose_item():
    pick = int(input("Enter what item number you want [1-20]: "))
    store_item(pick)
    while pick < 1 or pick > 20:
        print("Not Valid")
        pick = input("Enter what item number you want [1-20]: ")


def store_item(pick):
    items =['1','1','1','1','1','2','2','2','2','2','3','3','3','3','3','4','4','4','4','4','5','5','5','5','5','5','5','5','5','5','6','6','6','6','6','7','7','7','7','7','8','8','8','8','8','9','9','9','9','9','9','9','9','9','9','10','10','10','10','10','11','11','11','11','11','12','12','12','12','12','13','13','13','13','13','14','14','14','14','14','15','15','15','15','15','16','16','16','16','16','17','17','17','17','17','18','18','18','18','18','19','19','19','19','19','20','20','20','20','20']
    if pick == 1:
        items.remove(items['1'])

choose_item()

1 个答案:

答案 0 :(得分:0)

您正在使用字符串作为整数,在这里items.remove(items ['1']),remove不能那样工作。 选中此Difference between del, remove and pop on lists