hangman/hangman.py

72 lines
1.9 KiB
Python

#!/usr/bin/env python
"""
An implementation of the game Hangman.
"""
import random
word_file = open("words.txt")
words = word_file.readlines()
word = random.choice(words).strip()
correct_guesses = []
incorrect_guesses = []
print("""
_ _
| | | |
| |__| | __ _ _ __ __ _ _ __ ___ __ _ _ __
| __ |/ _` | '_ \ / _` | '_ ` _ \ / _` | '_ \
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
__/ |
|___/
With over 20,000+ words!""")
def print_string():
string = ""
for letter in word:
if letter in correct_guesses:
string += letter.upper() + " "
else:
string += "_ "
print(string)
def main():
print("")
while len(incorrect_guesses) < 6:
print("")
print_string()
guess = raw_input("Your guess: ").lower()
if guess in correct_guesses or guess in incorrect_guesses:
print("You've already guessed: " + guess)
if guess in word:
print("You guessed correctly! " +
guess.upper() + " is in the word.")
correct_guesses.append(guess)
won = True
for letter in word:
if letter not in correct_guesses:
won = False
if won:
print("Congratulations you discovered the mystery word and won!")
break
else:
incorrect_guesses.append(guess)
print("Incorrect! " + guess.upper() +
" is not in the word! You have " +
str(6 - len(incorrect_guesses)) +
" guesses left.")
print("The mystery word was " + word.upper() + ".")
print("Game Over!")
if __name__ == "__main__":
main()