Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hangman Game
- #
- # The classic game of Hangman. The computer picks a random word
- # and the player wrong to guess it, one letter at a time. If the player
- # can't guess the word in time, the little stick figure gets hanged.
- # imports
- import random
- # constants
- HANGMAN = (
- """
- ------
- | |
- |
- |
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- |
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | -+-
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- |
- |
- |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- | |
- | |
- | |
- |
- ----------
- """,
- """
- ------
- | |
- | O
- | /-+-/
- | |
- | |
- | | |
- | | |
- |
- ----------
- """)
- print "Welcome to Hangman. Good luck!"
- MAX_WRONG = len(HANGMAN) - 1
- word_list = []
- use_own_words = raw_input("would you like to add any of your own words? [y/n]")
- use_own_words = use_own_words[0]
- use_own_words = use_own_words.upper()
- dont_add_word = "false"
- if use_own_words == "Y":
- own_word = raw_input("type a word to add or leave blank to finish adding ")
- while own_word != "":
- own_word = own_word.upper()
- for i in range(len(word_list)):
- if own_word == word_list[i]:
- dont_add_word = "true"
- if dont_add_word == "false":
- print "added"
- word_list.append(own_word)
- else:
- print "you already added that word"
- dont_add_word = "false"
- own_word = raw_input("next word ")
- guess = raw_input("\nEnter your guess: ")
- # initialize variables
- if word_list == "":
- WORDS = ("OVERUSED", "CLAM", "GUAM", "PUCK", "TAFFETA", "PYTHON")
- else:
- WORDS = word_list
- word = random.choice(WORDS) # the word to be guessed
- #word = WORDS[4] # For debug purposes only TODO : Remove
- so_far = "-" * len(word) # one dash for each letter in word to be guessed
- wrong = 0 # number of wrong guesses player has made
- used = [] # letters already guessed
- while wrong < MAX_WRONG and so_far != word:
- print HANGMAN[wrong]
- print "\nYou've used the following letters:\n", used
- print "\nSo far, the word is:\n", so_far
- guess = raw_input("\n\nEnter your guess: ")
- while guess == "":
- guess = raw_input("\n\nEnter your guess: ")
- guess = guess[0]
- guess = guess.upper()
- while guess in used:
- print "You've already guessed the letter:", guess
- guess = raw_input("Enter your guess: ")
- guess = guess.upper()
- used.append(guess)
- if guess in word:
- print "\nYes!", guess, "is in the word!"
- # create a new so_far to include guess
- new = ""
- for i in range(len(word)):
- if guess == word[i]:
- new += guess
- else:
- new += so_far[i]
- so_far = new
- print HANGMAN[wrong]
- print "\nYou've used the following letters:\n", used
- print "\nSo far, the word is:\n", so_far
- answer = raw_input("would you like to guess the word? [y/n]")
- answer = answer.upper()
- if answer[0] == "Y":
- guess_word = raw_input("enter word: ")
- guess_word = guess_word.upper()
- if word == guess_word:
- so_far = guess_word
- else:
- print "\nSorry,", guess_word, "isn't the word."
- wrong += 1
- else:
- print "\nSorry,", guess, "isn't in the word."
- wrong += 1
- if wrong >= MAX_WRONG:
- print HANGMAN[wrong]
- print "\nYou've been hanged!"
- else:
- print "\nYou guessed it! \o,"
- print "\nThe word was", word
- raw_input("\n\nPress the enter key to exit.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement