Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Struggling with nested list
#1
Now I’ve gotten a slight grasp on dictionaries ,the join() and split I am working on understanding nested list and how to process them at least I think that that is my problem at this point here is the code I’m playing around with It’s supposed to print location and a list of directions then ask for input as to which direction do you want to go but I get stuck with it repeating the same location never changing.
I got it going once by correcting an error in exits but when I typed it out again I get the same
Error.

location={
	0:'you are at home',
	1:'you are on a road' ,
  2:'you are on a hillside',
  3:'you are looking at a lake',
  4:'you are in the country' ,
  5:'you are in outer space! ' }

exits=[
{'q':0},
{'w':2,'e':3,'n':5,'s':4,'q':0},
{'n':5,'e':1,'q':0},
{'w':1,'q':0},
{'n':1,'w':2,'q':0},
{'w':2,'s':1}]

loc=1
while True:
	avail_exits=','.join(exits[loc].keys())
	print(location[loc])
	if loc == 0:
		break
	direction=input('what direction do you want to go in?' + avail_exits)
	if direction in exits[loc]:
		loc == exits[loc][direction]
	else:
		print('you cannot go in that direction')
Reply
#2
In line 25 the "=="" operator examines true or false.
Or should it be an assignment?

Paul
Reply
#3
That is the mistake in the code.I caught it shortly after making this post, I thought that I made a mistake in the processing of exits[loc] , since that nesting list/tuples
Is what I’m now trying to grasp an understanding of. Is that what
exits[loc][direction] is, Processing of a nested list?
Reply
#4
(Jul-09-2020, 05:18 PM)gr3yali3n Wrote: Is that what exits[loc][direction] is, Processing of a nested list?

exits is a list with 2 (variable) dimensions. Zero based.
exits[loc][direction] means (in exits) element number "loc" , and of that element the dictionary item that corresponds with "direction" .

Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  shuffle a nested list giorgosmarga 11 403 Nov-12-2020, 07:04 PM
Last Post: perfringo
  Struggling for the past hour to define function and call it back godlyredwall 2 267 Oct-29-2020, 02:45 PM
Last Post: deanhystad
Question Save list with nested list into CSV SpongeB0B 1 364 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  struggling with != statements CallumRoberts2004 2 273 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  I’m Flat out struggling to understand list indexes gr3yali3n 7 521 Jul-20-2020, 07:18 PM
Last Post: princetonits
  Nested Dictionary/List tonybrown3 5 720 May-08-2020, 01:27 AM
Last Post: tonybrown3
  Help removing asterisk item in a nested list. bmcguire 3 534 Apr-06-2020, 02:35 PM
Last Post: snippsat
  Make nested system directories based on an unsorted list? koebi 0 415 Mar-25-2020, 01:14 PM
Last Post: koebi
  Struggling to exit this while loop fatherted99 5 566 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  Struggling with several while loops nsadams87xx 1 436 Nov-25-2019, 02:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020