I'm new here in the world of coding and I haven't received a very warm welcome. I've been trying to learn python via the online tutorial http://learnpythonthehardway.org/book/. I've been able to struggle my way through the book up until exercise 48 & 49. That's where he turns students loose and says "You figure it out." But I simply can't. I understand that I need to create a Lexicon of possible words and that I need to scan the user input to see if it matches anything in the Lexicon but that's about it! From what I can tell, I need to create a list called lexicon:
lexicon = [
('directions', 'north'),
('directions', 'south'),
('directions', 'east'),
('directions', 'west'),
('verbs', 'go'),
('verbs', 'stop'),
('verbs', 'look'),
('verbs', 'give'),
('stops', 'the'),
('stops', 'in'),
('stops', 'of'),
('stops', 'from'),
('stops', 'at')
]
Is that right? I don't know what to do next? I know that each item in the list is called a tuple, but that doesn't really mean anything to me. How do I take raw input and assign it to the tuple? You know what I mean? So in exercise 49 he imports the lexicon and just inside python prints lexicon.scan("input") and it returns the list of tuples so for example:
from ex48 import lexicon
>>> print lexicon.scan("go north")
[('verb', 'go'), ('direction', 'north')]
Is 'scan()' a predefined function or did he create the function within the lexicon module? I know that if you use 'split()' it creates a list with all of the words from the input but then how does it assign 'go' to the tuple ('verb', 'go')?
Am I just way off? I know I'm asking a lot but I searched around everywhere for hours and I can't figure this one out on my own. Please help! I will love you forever!
I wouldn't use a list to make the lexicon. You're mapping words to their types, so make a dictionary.
Here's the biggest hint that I can give without writing the entire thing:
lexicon = {
'north': 'directions',
'south': 'directions',
'east': 'directions',
'west': 'directions',
'go': 'verbs',
'stop': 'verbs',
'look': 'verbs',
'give': 'verbs',
'the': 'stops',
'in': 'stops',
'of': 'stops',
'from': 'stops',
'at': 'stops'
}
def scan(sentence):
words = sentence.lower().split()
pairs = []
# Iterate over `words`,
# pull each word and its corresponding type
# out of the `lexicon` dictionary and append the tuple
# to the `pairs` list
return pairs