Could anyone give me some assistance with the following code? Everything is fine except one minor error. Basically, the code asks the user for a word. The word is taken and then masked. For example, let's say I enter the word: football
football is then converted to ** (one * for each letter). After that, the code will ask the user for a number of guesses to attempt. Let's say I enter 8 (exactly how long the word football is).
After 8 is entered, the user will be asked to give a guess 8 times, with each correct guess updating the masked string to show the guessed letter. The problem is that I want the program to automatically end right after the word has been revealed. For example, with football, each time a duplicate letter (ex. o and l) is entered, two letters are revealed and a guess attempt is skipped. So after football is entirely unmasked, the code/program still asks for 2 additional guesses. I don't want these two additional guesses, I want the program to end immediately. But I can't figure out how to do it.
I tried to set the following command at the end
if masked_secret_word == secret_word: break
But that doesn't seem to do anything. I thought it would match the fully guessed word with the original word and then end it but that doesn't seem to work. Anyone able to provide some assistance? Thank you.
def word_update (secret_word, guessed_letters):
masked_secret_word = ""
for letter_guess in secret_word:
if letter_guess in guessed_letters:
masked_secret_word += letter_guess
else:
masked_secret_word += "*"
print "Secret Word update:" + masked_secret_word
alphabet = map(chr, range(97, 123))
secret_word = raw_input ("Type in the secret word: ").lower()
while secret_word.isalpha() == False:
secret_word = raw_input ("ERROR: Type in the secret word").lower()
masked_secret_word = len(secret_word) * "*"
guesses = raw_input ("How many guesses will you allow?")
while guesses.isdigit() == False or 0 >= int(guesses):
guesses = raw_input ("ERROR: How many guesses will you allow?")
guesses = int(guesses)
while len(secret_word) > guesses:
guesses = raw_input ("ERROR: The number of guesses is smaller than the word")
while guesses.isdigit() == False or 0 >= int(guesses):
guesses = raw_input ("ERROR: How many guesses will you allow?")
guesses = int(guesses)
print "Secret Word = " + masked_secret_word
guessed_letters = []
while guesses != 0:
letter_guess = raw_input ("Guess a letter (a-z)? ")
while letter_guess not in alphabet:
letter_guess = raw_input ("ERROR: Guess a letter (a-z)? ")
if letter_guess in guessed_letters:
print "That letter has already been guessed."
else:
guesses = guesses - 1
guessed_letters.append(letter_guess)
word_update(secret_word, guessed_letters)