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

In ps5, I cannot get "def is_valid_word" from mutating my "hand". Even if I use temphand = hand.copy(), it still mutates my hand dictionary. does anyone know how to stop this?

OpenStudy (anonymous):

hand is a frequency dictionary? dict.copy() should work: ``` >>> d = {'a':1, 'b':2} >>> b = d.copy() >>> d == b True >>> d is b False >>> ``` post your code please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste.

OpenStudy (anonymous):

hand == temphand is true. hand is temphand is false. http://dpaste.com/1408980/ even though I only use temphand in the is_valid_word function, temphand does not mutate, only hand mutates!

OpenStudy (anonymous):

When using a for loop, you can iterate over the elements of a sequence directly, you don't have create indices then index into the sequence. See the changes I made to available_letters(). I also made a comment at line 24, it might help your problem. http://dpaste.com/1409714/ http://docs.python.org/2.7/tutorial/controlflow.html#for-statements http://www.diveintopython.net/file_handling/for_loops.html http://effbot.org/zone/python-for-statement.htm

OpenStudy (anonymous):

I really appreciate your response. I really like the adjustments you made to the for loop. I didn't realize that could be done, it makes things much nicer and far less confusing. It turns out that the reason that this code wasn't working is that I used the "temphand = hand.copy()" outside the function that uses "temphand" and then passed it into the function. For some reason, I still don't know why, when I do this it keeps changing the original "hand" dictionary. This mutation doesn't happen if I put the "temphand = hand.copy()" within the function that uses it.

OpenStudy (anonymous):

So you create a new variable temp_hand at line three, this is a copy of hand and it is the one you want to use. At line 25, what do you pass to available_letters() ``` a = available_letters(word, hand) ```

OpenStudy (anonymous):

yes, I created the variable temp_hand at line three, but it won't work if I do that. For it to work, I need to wait until line eight and create temp-hand there. At line 25, I am passing True or False statements to the variables. The function is to test to see if a word passes two word tests. The two functions within the function are just the two different word tests. Then if both of them are True, line 30 returns a True for the encompassing function.

OpenStudy (anonymous):

nope. you create a copy, temp_hand, then you pass the original hand to available_letters, that is why it was mutating the original hand.

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!