如何根据项目条件删除字典条目

时间:2014-11-19 04:36:24

标签: python dictionary text-based

我希望从游戏开始时我的字典中删除一个房间,而雪地靴=假。当snowboots = True时,我希望房间可以到达,我想拿起雪地靴让它们变成真实。

如果这是有道理的。

roomDirections = {
    "hallEnt":{"e":"hallMid"},
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
    "snowRoom":{"n":"hallMid"},
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"},
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"},
    "riverBank":{"e":"lilyOne", "w":"giantNature"},
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"},
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"},
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"},
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"},
    "treasureRoom":{"w":"hallEnt"},
}


roomItems = {
    "hallEnt":["snowboots"],
    "snowRoom":["lamp"],
    "treasureRoom":["treasure"],
    }

snowboots = lamp = treasure = False

这些是我的词典和我所谓的变量。

if "snowboots" == False:
            del roomDirections["hallMid"]
        else:
            print ("you cannot go that way")

这是为了将hallMid从roomDirections移除,所以从那里移动是不可能的,直到......

elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)
        playerInput == True
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

snowboots = True,这就是这个大块的假设,但它似乎不起作用,我是关闭还是完全偏离轨道?

编辑 - 我的主要游戏循环 -

while True:
    playerInput = input("What do you want to do? ")
    playerInput = playerInput.lower()
    if playerInput == "quit":
        break

    elif playerInput == "look":
        print(roomDescriptions[currentRoom])




    elif playerInput in dirs:
        playerInput = playerInput[0]
        if playerInput in roomDirections[currentRoom]:


            currentRoom = roomDirections[currentRoom][playerInput]
            print(roomEntrance [currentRoom])
        else:
            print("You can't go that way")

    elif playerInput == "lookdown":
        if currentRoom in roomItems.keys():
            print ("You see", roomItems[currentRoom])
        else:
            print ("You see nothing on the ground")

    elif playerInput == "inventory" or playerInput == "inv":
        print (invItems)




    elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)       
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

    elif playerInput in invItems:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        for i in range (0, len(invItems)):
            if playerInput == invItems[i]:
                del invItems[i]
                break
    else:
        print ("I don't understand")

3 个答案:

答案 0 :(得分:1)

据我了解,您想添加一些条件来决定是否允许通过特定出口。您目前有字典映射每个房间的路线,用于确定玩家是否拥有每个项目的变量,以及每个房间中的项目列表和玩家库存。请注意,项目变量是多余的;你可以简单地查看库存。

您建议的方法是在获取或丢失所需物品时添加和删除房间的出口。这可以做到,但是找到哪个出口要删除的复杂性就是你首先要忽视它们的所有内容;如果将它们移除则恢复它们比根据需要过滤它们更难。这是一种方法:

requirements = {'snowRoom': 'snowboots', 'darkCave': 'lamp'}
reasons = {'snowboots': "You'd sink into the snow.",
           'lamp': "It would be too dark to see."}

如果条件不满足,您可以使用这些来忽略方向:

elif playerInput in dirs:
    playerInput = playerInput[0]
    if playerInput in roomDirections[currentRoom]:
        newRoom = roomDirections[currentRoom][playerInput]
        required = requirements.get(newRoom)
        if required and required not in invItems:
            print("You can't go that way. " + reasons[required])
        else:
            currentRoom = newRoom
            print(roomEntrance [currentRoom])
    else:
        print("You can't go that way")

你也可以这样做,这样玩家就无法移除房间里所需的物品:

elif playerInput in invItems:
    if playerInput != requirements[currentRoom]:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        invItems.remove(playerInput)
    else:
        print("You still need " + playerInput + ". " + reasons[required])

有一个更面向对象的方法可能是有意义的,其中房间实际上包含其项目列表,到其他房间的链接和要求。您还可以执行诸如向项添加同义词之类的操作。

答案 1 :(得分:0)

让我在这里指出你的错误。您有snowboots(这是一个对象),但您正在比较"snowboots"(这是一个字符串)。因此,当您在"snowboots"条件下比较if时,它将返回false,因此它将始终转到else部分。请尝试以下代码,并告诉我它是否对您有所帮助。

snowboots = False
if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

编辑: 为了更加清晰起见,我编辑了代码以显示del有效。我已删除

roomDirections = {
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
}

print roomDirections  ## print dictionary before your deleting.

snowboots = lamp = treasure = False

if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

print roomDirections                ## print dictionary after deleting

输出:

    {'hallMid': {'s': 'snowRoom', 'e': 'giantNature', 'w': 'hallEnt'}}   ## before deleting

     {}     ## after deleting.

答案 2 :(得分:0)

playerInput是一个字符串。看起来你需要将字符串映射到布尔值。

items = {'snowboots' : False,
         'lamp' : False,
         'treasure' : False}

然后改变

if "snowboots" == Falseif not items['snowboots']

playerInput == Trueitems[playerInput] = True

snowboots = lamp = treasure = False

for item in items:
    items[item] = False

看起来你对字符串和名称有一个概念上的问题,似乎在你的代码中将它们混合起来。 "foo"foo不同。

>>> 
>>> foo = 2
>>> "foo" = 2
SyntaxError: can't assign to literal
>>> foo = "foo"
>>> foo
'foo'
>>> "foo" = foo
SyntaxError: can't assign to literal
>>> foo = False
>>> bool("foo")
True
>>> bool(foo)
False
>>>

您需要完成所有代码并解析所有类似的代码。