Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Vocabulary indexing?
#1
So I thought that I had this figured out, I was mistaken, I’ve been using this piece of code to practice learning dictionaries, indexes, join and split methods.
Now I’m adding some vocabulary to the list of choices and I don’t see why my code doesn’t execute properly. It will just break as soon as I enter a word like ‘north’ it’s supposed to go north when north is typed.

location={
	0:'home',
	1:'road',
	2:'hill',
	3:'building',
	4:'lake',
	5:'mountains',
}

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

vocab={
	'quit':'q',
	'north':'n',
	'west':'w',
	'east':'e',
	'south':'s',
}
loc = 1

while True:
	avail_exits = ','.join(exits[loc].keys())
	print(location[loc])
	if loc == 0:
		break
	direction=input('what way?'+avail_exits)
	
	if len(direction)>1:
		for words in vocab:
			if words in direction:
				loc = vocab[words]
				
	if direction in avail_exits:
		loc=exits[loc][direction]
	else:
		print('almost figured it out')
		
P.s.
I figure that a line might be wrong In the for loop bu even still the output just repeats lake.

location={
	0:'home',
	1:'road',
	2:'hill',
	3:'building',
	4:'lake',
	5:'mountains',
}

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

vocab={
	'quit':'q',
	'north':'n',
	'west':'w',
	'east':'e',
	'south':'s',
}
loc = 1

while True:
	avail_exits = ','.join(exits[loc].keys())
	print(location[loc])
	if loc == 0:
		break
	direction=input('what way?'+avail_exits)
	
	if len(direction)>1:
		for words in vocab:
				direction = vocab[words]
				
	if direction in avail_exits:
		loc=exits[loc][direction]
	else:
		print('almost figured it out')
Reply
#2
Instead of
    if len(direction)>1:
        for words in vocab:
            if words in direction:
                loc = vocab[words]
you could use
direction = vocab.get(direction, direction)
which tries to get a value from the dictionary whos key is direction, if there is no direction key it defaults to the original direction
Reply
#3
direction (after the input) is a string.

On line 37 you say if words in direction. That means it treats direction as an iterator, and it tries to see if "words" matches any of the letters in direction (they won't). You probably mean if words == direction

But if you do that it will set loc to vocab[words]. If you typed "north", then this would be "n". You can't use "n" as an index and exits[loc] will error because it doesn't know what to do with an "n".
Reply
#4
This is the third time in as many days that a post is iterating through keys to determine if an input is a valid key. Kind of defeats the purpose of using a dictionary.

'north' in 'north' is True, so is 'nor' in 'north' or any substring of 'north' in 'north'. Though it will work, it is an odd way to compare strings and readers of you code will find it confusing.

I like the way Yoriz uses the vocab dictionary, but I fear the example may be confusing. Putting in the context of your code:
    direction = input('what way?')
    direction = vocab.get(direction, direction) # Changes direction only if found in vocab
    newloc = exits[loc].get(direction, None)    # Return None if not found
    if newloc is None:
        print("You can't go that way")
    else:
        loc = newloc
Reply
#5
Yes, vocabulary .get(direction) works for what I needed,
Also I corrected my problem by adding an string word=direction
Before the for loop like so

	word=direction
	if len(direction)>1:
		for words in vocab:
				direction = vocab[word]			
Also I get it’s annoying that I keep asking these questions but I feel like every body has to start somewhere and if I don’t ask questions how am I supposed to learn.
There are most likely better ways to use a dictionary but at this point this is what the instructor is doing so I’m following along but trying to grasp a complete understanding before moving on to another section.
Reply
#6
(Jul-15-2020, 11:19 PM)gr3yali3n Wrote: Also I get it’s annoying that I keep asking these questions but I feel like every body has to start somewhere and if I don’t ask questions how am I supposed to learn.
There are most likely better ways to use a dictionary but at this point this is what the instructor is doing so I’m following along but trying to grasp a complete understanding before moving on to another section.
I wonder if all three posts can be traced back to the same instructor.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,327 Jan-22-2020, 05:13 PM
Last Post: Ruthra

Forum Jump:

User Panel Messages

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