Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

This is a code fragment from Hangman game in Problem Set 2, 6.00. It works fine but I feel like I am using one too many loops. word is a str guess is a list of length len(word) c is a char that we are testing if c in word: for i in range(len(word)): if c == word[i]: guess[i] = c print "Good guess: " + "".join(guess) else: print "Oops! That letter is not in my word: " + "".join(guess) attempts -= 1

OpenStudy (rsmith6559):

As written, it's a good number of loops. It really only has the one for loop. The other "levels" are conditionals (if). Don't strings have a search method that will return the index of a match? Maybe that's in the string module. Your print statements are kind of klunky. I'd write it: print "Good guess: " + guess or print "Good guess", guess

OpenStudy (anonymous):

String index only returns the first instance, I am not sure how to implement a tupil of results effectively. It would be essentially the same what I do now I think. Guess is a list, so it prints gibbereish if I don't use join on it.

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!