繁体   English   中英

如何从字典中删除项目

[英]How do I delete items from a dictionary

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

问题是我的物品没有像应有的那样从“房间物品”中移除。

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

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")

从它如何遍历房间项目来看,似乎期望房间项目是一个字符串列表。 现在它们只是普通的字符串。 也许项目看起来应该更像这样:

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

还值得检查列表remove方法,因为它使您无需手动编写循环就可以从列表中删除内容。 例如:

invItems.remove(playerInput)

一次手工编写该循环以了解如何做当然是可以的,但是在那之后,使用内置方法可以使较大的程序更易于阅读。

编辑:在此行上也缺少右括号:

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

我通过运行脚本发现了这一点。 您的系统是否不报告错误的行号?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM