如何从字典中删除项目

时间:2014-11-18 03:30:53

标签: python

我正在复制我在课堂上为文本冒险任务编写的代码,但它的行为与我在课堂上编写的代码不同。

问题是我的物品没有从我的" roomitems"他们应该这样。

我只是一个初学者,所以请尽量让我保持简单:)

rooms = ["hallEnt", "hallMid", "snowRoom", "giantNature", "strangeWall", "riverBank"]
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"},
}

roomDescriptions = {
    "hallEnt":"The hallway continues east, the cave opening is to the north, there is an opening in the south wall, but its too high to reach, there are some snowboots on the ground.",
    "hallMid":"To the east the hallway grows dark, there is a corridor to the south",
    "snowRoom":"The room is a dead end except for a lit lamp on the ground",
    "giantNature":"To the east you see a river, to the south is a wall with some strange lines on it",
    "strangeWall":"It appears to be just a wall, there are some old lines on the wall but you can't make them out.",
    "riverBank":"The river has giant lilypads floating on its surface, however there is only one safe path across them, jump on the wrong lilypad and you will go through it and be swept back to the riverbank.",
    "lilyOne":"The lilypads all look the same, you can only guess which is the right direction",
    "lilyTwo":"The lilypads all look the same, you can only guess which is the right direction",
    "LilyThree":"The lilypads all look the same, you can only guess which is the right direction",
    "LilyFour":"The lilypads all look the same, you can only guess which is the right direction",
    "treasureRoom":"There is nothing in the room except for a pedistal with priceless treasures on top of it.",
}

roomEntrance = {
    "hallEnt":"You are standing in the hallway entrance.",
    "hallMid":"You walk to the middle of the hallway.",
    "snowRoom":"You are standing in a room as white as can be, and it is snowing inside the room.",
    "giantNature":"You walk into a gigantic forest with a river running through the middle",
    "strangeWall":"You walk over to the strange wall.",
    "riverBank":"You walk right up to the edge of the river.",
    "lilyOne":"You jump onto the first lilypad.",
    "lilyTwo":"You jump onto the second lilypad.",
    "lilyThree":"You jump onto the third lilypad.",
    "lilyFour":"You jump onto the fourth lilypad.",
    "treasureRoom":"You land safely on the far bank of the river.",
}

dirs = ["north","south", "east", "west", "n", "s", "e", "w",]

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

currentRoom = "hallEnt"
invItems = []


print("You are standing in a dark cave entrance")

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":
        print ("You see", roomItems[currentRoom])




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

    else:
        if 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")

1 个答案:

答案 0 :(得分:1)

根据它如何在房间物品中循环来判断它似乎期望房间物品是一个字符串列表。现在他们只是简单的字符串。也许这些项目应该更像这样:

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

还值得查看列表remove方法,因为它允许您从列表中删除内容而无需手动编写循环。例如:

invItems.remove(playerInput)

当然可以手动编写该循环以了解如何操作,但在此之后,使用内置方法可以使您的大型程序更易于阅读。

编辑:此行还缺少右括号:

for i in range(0, len(roomItems[currentRoom]):

我通过运行脚本找到了这个。您的系统是否未报告错误的行号?