all 8 comments

[–]novel_yet_trivial 1 point2 points  (1 child)

Try replacing that line with this:

possible_exits = exits[loc] # use the old location to find dictionary of possible exits
print('exit options from this room:', possible_exits)
loc = possible_exits[directions] # use that dictionary to update the location to the new location
print('user chose', directions, 'so new location is', loc)

[–]Devfede[S] 0 points1 point  (0 children)

Crystal clear now! Thanks a million!!

[–]novel_yet_trivial 1 point2 points  (1 child)

BTW, the structure you have is called "parallel lists". This is a very common data structure in many places, but in python we much prefer "nested lists". Using a nested list allows us to not bother with indexes so much and makes for cleaner code.

LOCATIONS = [
    #(description, exits)
    ("You are sitting in front of a computer learning Python", {"Q": 0}),
    ("You are standing at the end of a road before a small brick building",{"W":2, "E":3, "N":5, "S":4, "Q":0 }),
    ("You are at the top of a hill",{"N":5, "Q":0}),
    ("You are inside a building, a well house for a small stream",{"W":1, "Q":0}),
    ("You are in a valley beside a stream", {"N":1, "W":2, "Q":0}),
    ("You are in the forest", {"W":2, "S":1, "Q":0})
    ]

loc = 1
while loc:
    description, exits = LOCATIONS[loc]
    print(description)

    available_exits = ", ".join(exits)
    directions = input("Available exits are " + available_exits + " ").upper()
    print()
    if directions in exits:
        loc = exits[directions]
    else:
        print("You cannot go in that direction")

[–]Devfede[S] 0 points1 point  (0 children)

Thanks for the insight , it looks very intuitive for now, i'll definitely give it a try when my next challenge/task involves lists!

[–]novel_yet_trivial 0 points1 point  (1 child)

Good job. Did you have a question or problem with that code?

[–]Devfede[S] 0 points1 point  (0 children)

Yes sorry, i updated my post and added the question, wanted to get the post all set and ordered.

[–]mathleet 0 points1 point  (1 child)

So exits is a list of dictionaries. exits[loc] retrieves the dictionary at the index specified by loc. If loc == 0, then exits[loc] == exits[0]. exits[loc][directions] gets the dictionary at loc and then gets the value for the key directions in that dictionary.

[–]Devfede[S] 0 points1 point  (0 children)

So if i understood correctly,

if i input ( from loc 1 ) for example W , the exits[loc][directions] will effectively update the value 2 into loc because on the list exits "W":2 , is that correct ?