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

Does anyone have any tips on how to approach the Hangman problem in problem set 2? Specifically, I am having great difficulty building the "word solved so far" part. I can build something that looks like x__x__ but then cannot figure out how to make it into xy_xy_. Best, Thomas

OpenStudy (turingtest):

You should be saving all the letters you have guessed so far. Consider looping over the number of letters in the word, and for each letter location either placing a blank or the letter depending on whether or not 1 - the letter has been chosen 2 - the letter is belongs at that spot in the word so in pseudocode I am thinking something like the following: ``` #for each location in the number of letters in the word #if the letter that belongs there is in the set of guessed letters #place that letter in the location #otherwise #put a blank ```

OpenStudy (anonymous):

i am working on this at the moment as well . i am finding it a bit of a struggle to update my word . for example: random_word = struggle , random_word_index =[0,1,2,3,4,5,6,7] (made this by the looping the len of random word). Answer ongoing= ------- and a Guess = 'user input'. so when i guess , i take the letter guessed and look in random word and then i am looking to then change the ongoing word . i was using the index. function to get the first occurance of my guess. i was wanting to do something such as Answer_ongoing [random_word.index(Guess)]= Guess . what is the best way of using the index(guess). i know the first appearance is at a given number. i like to use this number to make the change. Thanks for you time jack

OpenStudy (turingtest):

My advice would be not to create an ongoing word and change it as you go, but rather to start with an empty string and progressively add letters and blanks. Something like ``` ongoing = '' #for each place in the number of letters of the word #if the letter of the word to guess has been guessed #add that letter to ongoing #otherwise #add a blank to ongoing #print ongoing ``` If you loop over the length of the word, you have an index already available in the form of your place in the loop. Consider using a `for` loop. How can you make a `for` loop repeat as many times as there are letters in the mystery word? (Hint: use `range`)

OpenStudy (anonymous):

Hi, To work with the ongoing word, you have to take in consideration that the data type is immutable. Therefore, you cannot use something like ongoingWord[index] = guessedLetter. The solution that I found was to create the string as ongoingWord=ongoingWord[:index] + guessedLetter + ongoingWord[index+1:] Regards, Vali

OpenStudy (anonymous):

@TuringTest, Thank you! I was missing the key insight to keep track of the guessed letters. It seems like an obvious point now, but I was stuck. Thanks again.

OpenStudy (turingtest):

You're very welcome :) I should point out that @Mahtar1984 's point is relevant: that strings are immutable in python, which is why having `ongoing` already a string and trying to modify it didn't work. Good luck on the rest of the course!

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!