Working on pset 6, problem #3. I've written a piece of code to find the highest scoring word, and while it's extremely inefficient, it appears as thought it should work. However it seems to be acting strangely. Here is the code: http://codepad.org/98LUXIJg And here is the piece of code that calls it: http://codepad.org/UCVI3zJV
Now the problem. Basically it appears as though when pick_best_word is running, it leaves prematurely, and jumps right to the next line of play_hand. I'll provide a sample output below: Loading word list from file... 83667 words loaded. Enter n to deal a new hand, r to replay the last hand, or e to end game: n Enter a time limit, in seconds for each players hand: 564 Current Hand: a a p c v x k leaving loop It took you 0.33 to provide and answer aa earned 6.01 points. Total: 6.01 points Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x leaving loop Invalid word, please try again. Current Hand: c k p v x
Ignore the "leaving loop" part, i just printed that before the return statement in pick_best_word to confirm it was leaving. so anyway, I just dont why it's leaving the loop, my intent is that pick_best_word runs until every single possible combination has been checked, but clearly it's working that way. Sorry for the long post, any help is appreciated!
line 2 - turning the hand into a list like that only works if there is only one of each letter: http://codepad.org/burMAtVa i commented everything out except your testwords and printed them - is this what you thought it was doing? http://codepad.org/FlA64fxu the 'words' you can make with {'a': 1, 'c': 1, 'b': 1} are a b c ac ca ab ba bc cb acb abc cab cba bac bca looks like you hit them all but there are some xtras - which is ok unless they turn out to be valid words looks like you could get to a point where pick_best_word can't make a word and returns an empty string, which you aren't 'handling'. put a print statement at line 37 of play_hand
I didn't realize I couldnt convert the dictionary like that, I;ll find an alternative way to do that. Yea there are extras, but after each testword formed, it checks the is_valid_word function which will make sure it is in the dictionary AND complies with the letters in the hand. I find it hard to believe that pock_best_word is actually "finishes" without finding a word for two reasons: 1 it's way too quick - I feel like a code like this will take at least a minute and 2 there are words to be found in the list of words. Anyway below is the output when putting a print statement at line 37, thanks so much for your help.
Loading word list from file... 83667 words loaded. Enter n to deal a new hand, r to replay the last hand, or e to end game: n Enter a time limit, in seconds for each players hand: 99 Current Hand: b u d h l o n line 37 Invalid word, please try again. Current Hand: b u d h l o n line 37 Invalid word, please try again. Current Hand: b u d h l o n
sorry i wasn't clear - i meant to print the userword that pick_best_word returns (at line 47 of play_hand) i pasted your two functions into my pset and it seems to work fine, except when pick_best_word cannot find a word with the letters in the hand. pick_best_word returns an empty string when it cannot find a word - you don't anticipate or handle that situation - you just categorize it as an invalid word and try again. maybe at line 3 of pick_best_word you can initialize winning_word to '.' or something else like 'i cant find a word' then in play_hand you can take action on that . if the hand dictionary has a letter with a frequency greater than one then using the dictionary keys() method won't give you a list that represents the hand - it works fine if all letter frequencies are one.
OK awesome it works now. The problem was that I was trying to use points_dict in is_valid_word to check if the word that computer found was valid. But I just reverted back to using word_list and it all works now. Thanks for helping me narrow it down!
Join our real-time social learning platform and learn together with your friends!