如果存在多于1个项目,则删除项目

时间:2016-09-14 15:30:21

标签: python

我正在处理一段代码,并且是一个小AI生物随机进入一个房间,并查看是否有任何东西。如果之前有任何玩家触摸过的内容,则需要该项目。它下一个房间会丢弃该项目,并可能会选择一个新项目。到目前为止,我有:

import random
rooms = ['kitchen', 'livingroom', 'basement']
itemsstatus = {'Umbrella': 1, 'Coin': 1}
itemsstatus['Umbrella'] = raw_input()
print "itemstatus['Umbrella']", itemsstatus['Umbrella']
roominventory = ['Umbrella', 'Coin']
goblininventory = ['baseball']
notpickedanythingelse = 'true'

gotoroom = random.choice(rooms)
if(gotoroom == 'kitchen') or (gotoroom == 'livingroom') or (gotoroom == 'basement'):
    ininventory = len(goblininventory)
    if(ininventory >= 1):
        roominventory.append(goblininventory[0])
        goblininventory.remove([0])
    else:
      print ""  

    for items in roominventory:
        if(itemsstatus[items] == 1) and (notpickedanythingelse == 'true'):
            goblininventory.append(items)
            roominventory.remove(items)
            notpickedanythingelse = 'false'
        else:
            print ""
    notpickedanythingelse = 'true'

    print roominventory
    print goblininventory

itemstatus [''] = rawinput()将由游戏自动完成,并且不会成为原始输入,它就在这里,所以我可以测试它。同样,每个房间都有它自己的库存和循环,但这只是为了它的简单性。妖精会拿起一件物品并保留它,但它不会掉落它已经拥有的物品(它一次只能携带1件物品)。如何才能获得它,以便在进入新房间时它会丢弃它所持有的物品?

1 个答案:

答案 0 :(得分:0)

goblininventory.remove([0])不正确。您应该使用goblininventory.pop()从列表中删除第一个元素。

有关removepop的更多信息,请参阅此处:https://docs.python.org/2/tutorial/datastructures.html#more-on-lists