Skip to content

Instantly share code, notes, and snippets.

@xenic
Created January 18, 2017 00:31
Show Gist options
  • Save xenic/2b8ad99c8babd4049025cc59c98f5ed4 to your computer and use it in GitHub Desktop.
Save xenic/2b8ad99c8babd4049025cc59c98f5ed4 to your computer and use it in GitHub Desktop.
2017_01_17 TIY-Durham Crash Course Hangman
import random
def get_hidden_word():
f = open("/usr/share/dict/words")
word_list = []
for line in f:
word_list.append(line.strip())
return random.choice(word_list)
def draw_board(hw, gl):
out = ""
for letter in hw:
if letter in gl:
out += letter + " "
else:
out += "_ "
print(out)
def get_guess_from_user(gl):
print("You have already guessed: %s" % (gl))
letter = input("guess a letter ")
return letter
def is_game_over(hw, gl):
#What are my end conditions?
#3 wrong guesses
wrong = 0
correct_count = 0
for guess in gl:
if guess not in hw:
wrong += 1
for letter in hw:
if letter in gl:
correct_count += 1
if correct_count == len(hw):
print("You WIN")
return True
if wrong >= 7:
print("You LOSE :(")
return True
else:
return False
def ask_user():
answer = input("do you want to play hangman? ")
return answer[0].lower() == "y"
guess_list = []
hidden_word = get_hidden_word()
draw_board(hidden_word, guess_list)
def play_game():
while not is_game_over(hidden_word, guess_list):
guess_list.append( get_guess_from_user(guess_list) )
draw_board(hidden_word, guess_list)
print("the word was: %s" % (hidden_word))
while ask_user():
play_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment