Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

Can someone explain why this is an incorrect approach to find the value of a word in the scrabble exercise? value_list = dict({'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}) word_index = 0 total = 0 def find_value(word): global word_index global total x = word[word_index] total = total + value_list[x] if len(word) > word_index + 1: word_index = word_index + 1 find_value(word)

OpenStudy (rsmith6559):

As much as I like recursion, this is an iterative problem. I don't see any need or advantage for word_index and total to be global variables. They'd only be used in find_value(). def find_value( word ): total = 0 for letter in word: if( letter in value_list ): total += value_list[ letter ] return total

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!