程序不会从列表中删除带变量的项目?

时间:2015-09-23 16:23:31

标签: python list

我正在编写一个程序,我需要根据人物搜索结果从列表中删除产品。 以下是我的代码:

 list = [['bike', 50], ['truck', 1000], ['microwave', 10]]
 while True:
    print "----------------------"
    print "Welcome to Craigslist" 
    print "Would you like to:" 
    print "1. Add an item."
    print "2. Find an item."
    print "3. Print the message board."
    print "4. Quit."
    choice = eval(raw_input("Please make a selection."))
    print "-----------------------"

#i took out choice 1 because the problem isn't in choice 1

if choice == 2:
    print "What are you looking for? A bike, microwave, dresser, truck or chicken?"
    itemType = raw_input ("Enter the item type- b, m, d, t, c,:")
    print list
    if itemType == "m":
        itemCost = eval(raw_input("Enter the maximum item cost:"))
        list.remove[('microwave',itemCost)]
        print list
    elif itemType == "d":
        itemCost = eval(raw_input("Enter the maximum item cost:"))
        list.remove('dresser',itemCost)
        print list
    elif itemType == "b":
        itemCost = eval(raw_input("Enter the maximum item cost:"))
        list.remove[('bike' ,itemCost)]
        print list
    elif itemType == "t":
        itemCost = eval(raw_input("Enter the maximum item cost:"))
        list.remove[('truck',itemCost)]
        print list
    elif itemType == "c":
        itemCost = eval(raw_input("Enter the maximum item cost:"))
        list.remove[('chicken',itemCost)]
        print list

if choice == 3:
    print "These are the items for sale on Craigslist."
    print list

if choice == 4:
    quit ("Bye!")

else:
    quit ("Sorry, that's not an option on the menu.")

当我尝试从列表中删除项目时,根据其名称和输入价格,我收到错误:

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

当我输入价格时bike我已经尝试完全重写行,切换括号和括号,50bike等于一个变量然后删除该变量 - 我似乎无法让它工作。我的业余编码大脑的任何输入将不胜感激! :)

1 个答案:

答案 0 :(得分:0)

建议:

  1. 更改变量名称' list'类似于' list1'

  2. 删除(('某些',费用))正在寻找删除元组但你有一个列表所以使用:remove([' something',cost])

  3. 3.使用elif而不是多个if语句

    4。'而True:'将一遍又一遍地继续运行缩进块,而不是继续执行if语句。

    我认为如果您进行这些更正,代码将执行您希望它执行的操作。希望能帮助到你!