There's something I don't understand in parser.py in ex49

1 def match(word_list, expecting):
2 if word_list:
3 word = word_list.pop(0)
4
5 if word[0] == expecting:
6 return word
7 else:
8 return None
9 else:
10 return None

It’s the code for match function above.

How I see the function is that the function
1.) takes a list of words and the expecting word type as arguments,
2.) takes the first word of the list,
3.) and compares the first letter of that first word to the expecting word type to check if the two are equal, and etc.

If I understood the code correctly, then the two should never be equal, right? Then isn’t there something wrong or am I missing something?

As far as I remember, the function takes as input a list of tuples.

See also the comment in the code below that is taken from the book:

1 class Sentence(object):
2
3 def __init__(self, subject, verb, obj):
4 # remember we take ('noun','princess') tuples and convert them
5 self.subject = subject[1]
6 self.verb = verb[1]
7 self.object = obj[1]

so, your word_list would be: [(‘noun’, ‘princess’), (‘verb’, ‘eats’)]

2 Likes

Also, @henry22 Welcome to the Forum! Keep up the good work!