Sep-05-2017, 01:35 PM
I'm going through this book on python 2.7 and I have to create some nosetests for these. So I have been commenting them, but I can't fully understand what the skip function does. So I gave it a list of tuples
skip([('noun', 'bear'), ('noun', 'cow'), ('noun', 'sheep'), ('noun', 'princess'), ('noun', 'boy')], 'noun')
and it only prints out the even tuples to the console. I can't seem to figure out why it does this.
Here is a snippet of my code.
skip([('noun', 'bear'), ('noun', 'cow'), ('noun', 'sheep'), ('noun', 'princess'), ('noun', 'boy')], 'noun')
and it only prints out the even tuples to the console. I can't seem to figure out why it does this.
Here is a snippet of my code.
def peek(word_list): """gets the word type in the first tuple of the list""" if word_list: # sets word to the first tuple word = word_list[0] # returns item at word's zero index return word[0] else: return None def match(word_list, expecting): """removes the specified word type""" if word_list: # pops off the first tuple in the list word = word_list.pop(0) # if item in word's index zero is equal to expecting if word[0] == expecting: # returns list without first tuple return word else: return None else: return None def skip(word_list, word_type): """skips specifed word type in a row until it reaches a new word type""" while peek(word_list) == word_type: print match(word_list, word_type) match(word_list, word_type)