How to remove dictionary entry depending on item condition

82 views Asked by At

I want a room to be removed from my dictionary from the start of the game while snowboots = False. When snowboots = True, I want the room to be reachable, and I want picking up the snowboots to make them True.

if that makes sense.

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

these are my dictionaries and my alleged variables.

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

this was meant to remove hallMid from roomDirections so movement from it is impossible, until...

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

the snowboots = True, which is what this chunk was suppose to do but it doesn't seem to be working, am I close or am I completely off track?

EDIT -- My main game loop --

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

There are 3 answers

0
Yann Vernier On BEST ANSWER

As I understand it, you want to add some conditions to decide whether going through a particular exit is allowed. You currently have dictionaries mapping the directions from each room, variables intended to hold whether the player has each item, and lists of items in each room and the player inventory. Note that the item variables are redundant; you could simply check the inventory.

Your proposed method is to add and remove exits from the rooms when the required items are acquired or lost. This can be done, but the complexity of finding which exits to remove is all you need to disregard them in the first place; restoring them if they were removed is harder than filtering them out as needed. Here's one approach:

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

You can then use these to ignore a direction if the condition was not satisfied:

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

You could also make it so players can't remove the required item in the room:

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

It may make sense to have a more object oriented approach, where a room actually contains its list of items, links to the other rooms, and requirements. You could also do things like add synonyms to items.

7
d-coder On

Let me point out your mistake here. You have snowboots (which is an object) but you are comparing "snowboots" (which is a string). So when you are comparing "snowboots" in if condition it'll return false and as a result it'll go to else part always. Try the following code and let me know if it helps you.

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

EDIT: For the sake of more clarity I've edited your code to show that del works. I have removed

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

Output:

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

     {}     ## after deleting.
0
wwii On

playerInput is a string. It looks like you need to map strings to boolean values.

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

then change

if "snowboots" == False to if not items['snowboots']

playerInput == True to items[playerInput] = True

snowboots = lamp = treasure = False to

for item in items:
    items[item] = False

Looks like you have a conceptual problem with strings and names and seem to be mixing them up in your code. "foo" is not the same as 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
>>>

You will need to go through all your code and resolve all similar code.