簡體   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