Skip to content

Instantly share code, notes, and snippets.

@samjarman
Created March 31, 2014 06:00
Show Gist options
  • Save samjarman/9886148 to your computer and use it in GitHub Desktop.
Save samjarman/9886148 to your computer and use it in GitHub Desktop.
A simple hangman game
import random
def print_word(word):
string = ""
for letter in word:
string += letter + ' '
print(string)
def get_word():
word_file = "/usr/share/dict/words" #only works for *nix users
words = open(word_file).read().splitlines()
return random.choice(words)
def run_game():
word = get_word()
progress = list("_" * len(word))
max_guesses = 10;
print_word(progress)
game_finished = False;
while not game_finished:
guess = raw_input()
if guess in word:
for i in range(len(word)): #for multiple letters
if guess == word[i]:
progress[i] = guess
else:
print("Nope, sorry!")
max_guesses -= 1
if progress.count("_") == 0:
print ("You win!")
game_finished = True;
if max_guesses <=0:
print("Game over!")
game_finished = True;
print("The word was: " + word)
print_word(progress)
if __name__ == '__main__':
run_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment