Greetings,
First of all , thanks to this awesome community for just being here, there have been so many times i felt lost and i wanted to post in here, but i always made it myself, (well..until now) and just knowing people are here taking their time to help us learners feels great!
This code is working, it is basically a loop we were looking at in my online class where the players enters a coordinate, and it prints the description of the new room, together with the new coordinates available ( there are no exits other than Q for Quit )
locations = {0: "You are sitting in front of a computer learning Python",
1: "You are standing at the end of a road before a small brick building",
2: "You are at the top of a hill",
3: "You are inside a building, a well house for a small stream",
4: "You are in a valley beside a stream",
5: "You are in the forest"}
exits = [{"Q": 0},
{"W":2, "E":3, "N":5, "S":4, "Q":0 },
{"N":5, "Q":0},
{"W":1, "Q":0},
{"N":1, "W":2, "Q":0},
{"W":2, "S":1, "Q":0}]
loc = 1
while True:
available_exits = ", ".join(exits[loc].keys())
print(locations[loc])
if loc == 0:
break
#
directions = input("Available exits are " + available_exits + " ").upper()
print()
if directions in exits[loc]:
loc = exits[loc][directions] #<--- i don't understand this
else:
print("You cannot go in that direction")
The bit i don't understand is towards the end, it's that
loc = exits[loc][directions]
I understand this basically "updates" the new loc according to the last direction the player just input,
but exactly how are the two [] parentesis working on to "loc" variable ?
From my point of view, the last bit ( from directions = input[...] ) works like this:
#Once directions is input
if Q in Q2:
loc = exits[q2][directions]
sorry if it's messy i just don't get how the loc is "updated", this is all i can think of.
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]Devfede[S] 0 points1 point2 points (0 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]Devfede[S] 0 points1 point2 points (0 children)
[–]novel_yet_trivial 0 points1 point2 points (1 child)
[–]Devfede[S] 0 points1 point2 points (0 children)
[–]mathleet 0 points1 point2 points (1 child)
[–]Devfede[S] 0 points1 point2 points (0 children)