from string import * #store state of game word_input = lower(raw_input("What is the secret word? ")) secret_word = word_input letters_guessed = [] result = ["-"]*len(secret_word) mistakes_made = 0 Max_guesses = 6 def get_guess(): guess = lower(raw_input("What letter would you like to guess? ")) # these lines test the input for validity if len(guess) > 1: print "You have entered more than one letter. Please guess again." # mistake() #enable this line if you want to penalize for incorrect input return if guess in letters_guessed: print "You have already guessed this letter. Try again." # mistake() #enable this line if you want to penalize for incorrect input return # after validating good input, the function actually records the guess letters_guessed.append(guess) if guess not in word_input: mistake() print "This letter is not in the word. You have made",mistakes_made,"mistakes." print_guessed_simple() return else: print "Congratulations, this letter is in the word." print_guessed(guess) def mistake(): global mistakes_made mistakes_made = mistakes_made + 1 def print_guessed(guess): global secret_word for i in range(secret_word.count(guess)): position = secret_word.find(guess) result.pop(position) result.insert(position,guess) secret_word_list = list(secret_word) secret_word_list.pop(position) secret_word_list.insert(position,"-") secret_word = join(secret_word_list, sep="") result_final = join(result,"") print result_final def print_guessed_simple(): result_final = join(result,"") print result_final #returns True if the player has won, returns False otherwise def word_guessed(mylist): correct = True for letter in mylist: if letter not in letters_guessed: correct = False return correct while word_guessed(word_input) == False: get_guess() if mistakes_made >= Max_guesses: print "You have lost!" raise SystemExit if word_guessed(word_input) == True: print "You have won!"