Problem set 5. Im developing my code for the get_word_score function but im having an issue. When running the test program I get results for some but not all the words. (py shot 1). When I change the line highlighted in grey this problem stops(py shot 2). Can anyone tell me why? I have not changed the test code and it was d/l'd from the MIT website
Here are the two screen shots that show my code and the test results
PLEASE NOTE, WHEN I REFER TO (py shot 1) I MEAN (pyshot2)!!! pys shot 2 = pyshot1 too!!!
It has to do with the way that arrays are indexed. len() returns the length of a word, let's say "steve", len(steve) would give 5 as a result however when you look at the character array that makes "steve" it is indexed as 0-4. So in your pyshot1 you are stepping through 0-5 which would actually be 6 places and your word, "steve" only has 5.
when the code says in range(0,wordlength - 1) it misses out the last letter of each word but displays every result, however, when it is in range(0,wordlength) it skips the result of some words - as seen in the two png files
To be honest I couldn't read the output on the right, it was too small and based on the premise of your question I took an educated guess. I will download the images so I can blow them up to see if I can locate your issue.
Well your problem is not in the get_word_score() method, it is printing the value and returning. You need to check, or post, the portion of code right before and after that method call.
This is the test code that I run to get the output shown in the screen shots. Thanks for your help and time on this :-)
From the looks of things, and assuming I understand your issue, the reason it is no longer printing the failure because this "if score != words[(word, n)]:" condition is no longer true. But I do not see, on either png, where it print "SUCCESS: test_get_word_score()"
i will put some extra code in the test code to see if i can gather more info on this..
Put a print statement before and after your call so that you know it is making it back to the main function.
here is the print out from running the file below. Im really lost here.. Loading word list from file... 83667 words loaded. ---------------------------------------------------------------------- Testing get_word_score... start test word len 8 current letter o value 1 total 1 current letter u value 1 total 2 current letter t value 1 total 3 current letter g value 2 total 5 current letter n value 1 total 6 current letter a value 1 total 7 current letter w value 4 total 11 current letter n value 1 total 12 about to return testing FAILURE: test_get_word_score() Expected 62 points but got '12' for word 'outgnawn', n=8 word len 6 current letter s value 1 total 1 current letter c value 3 total 4 current letter o value 1 total 5 current letter r value 1 total 6 current letter e value 1 total 7 current letter d value 2 total 9 about to return testing word len 2 current letter i value 1 total 1 current letter t value 1 total 2 about to return testing word len 0 returning 0 testing word len 7 current letter o value 1 total 1 current letter u value 1 total 2 current letter t value 1 total 3 current letter g value 2 total 5 current letter n value 1 total 6 current letter a value 1 total 7 current letter w value 4 total 11 about to return testing FAILURE: test_get_word_score() Expected 61 points but got '11' for word 'outgnaw', n=7 word len 3 current letter w value 4 total 4 current letter a value 1 total 5 current letter s value 1 total 6 about to return testing word len 7 current letter w value 4 total 4 current letter a value 1 total 5 current letter y value 4 total 9 current letter b value 3 total 12 current letter i value 1 total 13 current letter l value 1 total 14 current letter l value 1 total 15 about to return testing FAILURE: test_get_word_score() Expected 65 points but got '15' for word 'waybill', n=7 end test ---------------------------------------------------------------------- Testing update_hand... FAILURE: test_update_hand('quail', {'a': 1, 'i': 1, 'm': 1, 'l': 2, 'q': 1, 'u': 1}) Returned: None -- but expected: {'m': 1, 'l': 1} or {'a': 0, 'i': 0, 'm': 1, 'l': 1, 'q': 0, 'u': 0} ---------------------------------------------------------------------- Testing is_valid_word... FAILURE: test_is_valid_word() Expected True, but got False for word: 'hello' and hand: {'h': 1, 'e': 1, 'l': 2, 'o': 1} FAILURE: test_is_valid_word() Expected True, but got False for word: 'honey' and hand: {'e': 2, 'd': 1, 'h': 1, 'o': 1, 'n': 1, 'w': 1, 'y': 1} FAILURE: test_is_valid_word() Expected True, but got False for word: 'evil' and hand: {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} ---------------------------------------------------------------------- All done!
It seems like you missed something. You have this specification for get_word_score(): "The score for a word is the sum of the points for letters in the word, plus 50 points if all n letters are used on the first go." You missed the "plus 50 points if all n letters are used on the first go" part. You just have to add 2 lines of code in your get_word_score() function to implement this. Once you add that, I think it will solve your problem. :)
Well part of your error message is coming from here: ("waybill", 7):65, ("outgnaw", 7):61, ("outgnawn", 8):62 According to the scoring these should be ("waybill", 7):15, ("outgnaw", 7):11, ("outgnawn", 8):12 The lower part of your output is likely the result of those methods not being defined.
Hi kcpaas. I understand i havent added that part yet. i hadn't got that far when I came across this irregularity. I shall code it in and see if that helps - yet I still dont think it is answering my question. Thanks all the same tho
All sorted. Kcpass, you were right! thanks. I still dont understand why it wouldn't print a either a failure or success message for some of the words, but its working now anyways... Thanks for your help too espex
Oh! As for the success and failure messages, there is a problem with the test_get_word_score() function. This is the result when I tested my code: Testing get_word_score... start test word = outgnawn n = 8 testing SUCCESS: test_get_word_score() word = scored n = 7 testing SUCCESS: test_get_word_score() word = it n = 7 testing SUCCESS: test_get_word_score() word = n = 7 testing SUCCESS: test_get_word_score() word = outgnaw n = 7 testing SUCCESS: test_get_word_score() word = was n = 7 testing SUCCESS: test_get_word_score() word = waybill n = 7 testing SUCCESS: test_get_word_score() end test Apparently the code tests the words in this order: 'outgnawn', 'scored', 'it', '', 'outgnaw', 'was', 'waybill'. The test wasn't conducted in the order the words were written because this is a dictionary, not a tuple or list. Anyway, in your earlier code, you have failures in 'outgnawn', 'waybill', and 'outgnaw'. The problem is that in the for loop of the test code, once you get a failure, the value of the failure variable inside the loop will always be True in all the next iterations since the failure is not reset to False at the start of every iteration. Please refer to the attached code with my comments to better understand it. :) http://pastebin.com/tZp6bpXa Phew, that was a lengthy one but I hope it helps. :)
Join our real-time social learning platform and learn together with your friends!