PS.5. Hi all, I'm currently working on PSet 5. If any one wants to exchange ideas - and you're working on the same problems as I am - feel free to post!
Hello, Aslander! Has your question been answered?
I'm just starting the assignment now, but already I ran into a problem trying to run the ps5.py...not sure why either...I also tried changing the file name as the handout suggested, to indicate the entire path to the word.txt file, but that didn't work.
What's the issue you're having, or error you're getting?
2 issues right now: 1. When I run ps5.py I get an empty browser screen. 2. As the handout suggested, I changed the file name to reflect the full path to the wordlist. Still won't run. All files are in the same directory.
Ok Issues solved...I changed the path name to reflect the actual path to the file on my data drive, then typed an r", in front of my drive letter...it solved the problem and it now works as the handout indicates. Thanks guys!
More information about string literals can be found here: http://docs.python.org/reference/lexical_analysis.html#string-literals Of particular note is the raw strings and escape sequence information
Problem 1 was a success - now on to problem 2. I am reading it as: Make a dictionary of the HAND, the letters in HAND as the KEYS, and make their values equal to the amount of times they appear in HAND. Take the letters in WORD, then subtract them, or remove their keys from the HAND. Return remaining letters if any. Did anyone read that any differently?
The function gets the dictionary of the HAND as an argument . You just have to make a copy of it, modify it by removing/decrementing any letters that appear in WORD and return the newly updated/created hand.
Thanks - I rewrote some code and Problem 2 was successful! I totally read it incorrectly. I'll have to be more thorough for Problem 3 now.
So Prob 3 was a success...Prob 4 is giving me trouble. I can get the prog to quit after typing the "." and I can get the hand dealt, and iterate if the word is not valid. However, if I type a valid word, it returns an empty prompt, and I am out of the loop. My call to "update_hand" seems to be erroneous...and I thought it would be straightforward!
Problem 4 - not so easy. So far I can display the hand, and while the word is in wordlist, update word while printing the score. Now how to quit the loop and return the grand score?
return often works well
Indeed...I think I need a rest...I'm starting to dream in code.
Still working at this - no success yet. If I can't get this thing working soon I'll have to go back and start at step one!
What've you got so far?
My functions have all been successful. However, when I get to the play hand function...which is problem 4, I suck out. It seems that I don't have the functions calling in the correct sequence, with the proper arguments. I'll paste some code in a few...
These are the functions I wrote, which have all succeeded. http://dpaste.com/460177/
I see a few bugs in your approach. Firstly in get_word_score you need to award the bonus 50 points if len(word) >= n, not if n and len(word) are >= 7. Secondly, update hand is supposed to return a new hand with the letters removed, it is not supposed to alter the hand it's given. Third issue. You are altering the hand in your is_valid_word function. Don't do this. You should be comparing the letter counts in word to the counts in hand, but not decrementing the count from the hand. Try fixing these bugs and see if you get the right behavior.
Ok - I'll try doing that and see what happens. Thanks.
So how am I supposed to return the new hand without mutating it? I only decrement the values for each key, and then return the keys with >0 values. Is that the same as mutating the hand?
I think it works now - I tried playing but I haven't gotten to the point where display_hand = "". http://dpaste.com/463507/
In answer to your question about mutating it. Yes, that is changing the hand you are given. The easiest way to prevent mutating the original hand is to make a copy of it. new_hand = dict(hand) then work with new_hand (and return it) while leaving hand alone.
while display_hand != "": this will not do what you intend. display_hand is a function. Functions are not comparable with strings in any sensible way, and it doesn't return anything either, so don't try changing it to display_hand(hand) != ''. hand == "" is also not a comparison you should be doing, (maybe you want to check if the dictionary is empty?). Please elaborate further on what your thought process is for this algorithm.
Checking if Dict is empty...now why didn't I think of that! I was going all over the place - check for values being 0, or not, etc. Good idea polpak! I did get all the functions to work, except the automatic quit when update_hand returns the empty string. My idea was that if the displayed hand was empty, then it would execute the quit or ".". So if I set a condition that checks if display_hand returns nothing, then it would execute the QUIT. I did realize however, earlier today, that it wouldn't work, but couldn't figure out what to do next. Thanks - I'll try something and see what happens.
So I finally got the empty hand issue resolved, and the program seemed to work like a charm, except I get this one bug that I think is due to the hand still being mutated, during the update_hand cycle.For some reason, if I use a letter in the word once, that occurs twice in the hand, when I update, it removes both letters. Ex. If the display hand has two r's, and I use one, when I update, both r's disappear. But that only happens occasionally...or when the letters appear at the beginning of the hand...which is beyond me. Any ideas why that would be?
I'd need to see your current version of the code.
So here is my code for the play_hand and update...which I think may be the problem...but the rest of the script works well. I get the proper return score when there are no double letters, etc. http://dpaste.com/473845/
As well, my quit if/else statement works properly, as it stops the current hand, returns the total score, and starts play_game.
your update hand function is still mutating the hand it's givin you need to fix that
I just changed something and it seemed to have fixed that bug: My 2nd while statement - changed from while to elif: elif is_valid_word(word, hand, word_list) == True: I checked a hand with two l's and used one. It returned it properly...but I haven't seen all other scenario's.
This is an example of a non-mutating update_hand function http://dpaste.com/473872/
Ok so basically you take a count of the letter in word, then use that as a value in the new dict, newhand. Isn't this really the same, as my newhand is not hand? newhand = hand for i in word: newhand[i] = newhand.get(i,0) - 1 return newhand
Or rather, my hand is not being mutated, but only my newhand is to return the update?
hand is being mutated in your example because newhand is the same dictionary as hand. in my example, newhand is a brand new dictionary which we add keys to and return.
in your example the names newhand and hand refer to the same object.
Ooooooohhhhhhh - ic the light now. :-) Thanks for your patience!
of course! =)
Ok - worked on the GHOST assignment and it works so far...no bugs yet!
Gratz!
Join our real-time social learning platform and learn together with your friends!