This is my take on a simple hangman game, which is played from command prompt.
import random wordlist =  word = random.choice(open("words.txt").readlines()) #word.txt is a list of words, in the same directory with the game file. length = len(word) mock_word = "_ " * length #The underscores presented to the player mock_word = mock_word.split() #making it a list, since strings can't be assigned used_lets =  lives = 7 while True: print " ".join(mock_word) #Needs space in join, so the underscores are separated. let = raw_input("Give a letter: ") if len(let) > 1: #User can only give one letter print "Only one!" elif (let not in word): if let not in used_lets: lives = lives - 1 if lives == 0: print "Sorry you lost, the word was:", word break print "Try again, you have only %r lives left!" % lives used_lets.append(let) print "So far you have used:" print used_lets else: for i in range(length): if let == word[i]: mock_word[i] = let if "".join(mock_word) == word: #Making it a string again print word + " You won!" break
If you want to test, just set word to something. That is how I tested the loop. I would appreciate criticisms of my coding technique, and perhaps the suggestion of an alternative of the while loop use, which feels wrong.