Posts: 53 Threads: 27 Joined: Apr 2020 location={ 0:'quit', 1:'town', 2:'beach', 3:'country side', 4:'city', 5:'outer space' } 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: avail_exits= ','.join(exits[loc].keys()) print(location[loc]) if loc ==0: break direction=input('available exits are ' + avail_exits) print() if direction in exits[loc]: loc=exits[loc][direction] else: print('you cant go that direction')So, I am taking a python class on udemy, I felt confident enough to move on to the next lecture and here is the code that is the topic, slightly different, but the code is on point. I understand most of it thanks to this forum and said class but I need to understand What is going on with .join(exits[loc].keys()) How is this portion of code working? The same thing with Loc = exits[loc][direction] Posts: 741 Threads: 122 Joined: Dec 2017 .join() makes a concatenated string of an iterable, in your case of a dictionary at place [loc] in a list of dictionaries. The elements are separated by a ','.join , comma in your example. This is done to present the list of choices in a nicer format. exits[loc][direction] : says that you need to take the dictionary number [loc] and specifically the value with key [direction] Paul It is more important to do the right thing, than to do the thing right.(P.Drucker) Better is the enemy of good. (Montesquieu) = French version for 'kiss'. Posts: 43 Threads: 0 Joined: Apr 2020 .join() actually is used to join strings.So let us focus on your code.The code 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 avail_exits= ','.join(exits[loc].keys()) print(avail_exits) output: Output: E,S,N,Q,W
So after using, the join statement made a string of all keys at location 1 and separated them by ',', which we can change too to anything.For example to keep just space between them we can use avail_exits= ' '.join(exits[loc].keys()) print(avail_exits Output: E S N Q W
Hope you got how join works Posts: 53 Threads: 27 Joined: Apr 2020 Yes, I understand how the join method works now. I still feel uncertain on how the code is working. For example If direction in exits[loc]: loc=exits[loc][dir] How does exits[loc][dir] have the position of player? Loc = location number Exits = north west etc. plus corresponding loc numbers Direction = user input So how does Loc = exits[loc][direction] Actually work? I’m sorry that I need everything spelled out in crayon right now. I need to go back and review a few things before moving on. Posts: 1,835 Threads: 2 Joined: Apr 2017 Jun-30-2020, 12:01 PM (This post was last modified: Jun-30-2020, 12:01 PM by ndc85430.) This shouldn't be hard: exits[loc] gives you the value in the list exits at the index specified by loc. That value is a dictionary, so exits[loc][direction] gives you the value in that dictionary associated with the key specified by direction. Posts: 53 Threads: 27 Joined: Apr 2020 Jul-07-2020, 09:01 PM (This post was last modified: Jul-07-2020, 09:01 PM by gr3yali3n.) But I’m still off on as to why I’m thinking .join() will place the additional string at the end\after the original string instead of one letter at a time while repeating the string? Like in string='a turtle sits on a log' string2='watching the sky' print(string.join(string2)) Posts: 1,144 Threads: 114 Joined: Sep 2019 Jul-07-2020, 09:36 PM (This post was last modified: Jul-07-2020, 09:38 PM by menator01.) string='a turtle sits on a log' string2='watching the sky' print(str.join(' ',(string, string2)))or string='a turtle sits on a log' string2='watching the sky' print(' '.join((string, string2)))Outputs in the order entered. Output: a turtle sits on a log watching the sky Posts: 1,589 Threads: 3 Joined: Mar 2020 join() takes 2 things: an iterator of items, and a string to put between the items. If you hand it a list in the iterator place, it joins the elements with the string. If you hand it a string in the iterator place (as in your example), it joins the individual characters with the other string. If you want to slap some strings together with join(), put them in a list and join them with a space or an empty string. string='a turtle sits on a log' string2='watching the sky' print(" ".join([string, string2]))Output: a turtle sits on a log watching the sky |