Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fun with hangman!
#6
(Jun-12-2017, 01:49 AM)Neosiq Wrote:
#My Simple hangman game
#Code by: Neosiq

#Original code by Michael Dawson

import random
from nltk.corpus import words
#You need to import and download nltk for this code to work

#You need these 2 things to run this program
#import nltk
#nltk.download()

word_list = words.words()

#Images of the hangman
hangman = [
    """
     ------
     |    |
     |
     |
     |
     |
     |
     |
    ----------
    """,
    """
     ------
     |    |
     |    O
     |
     |
     |
     |
     |
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /
     | 
     |   
     |   
     |   
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /|
     |   
     |   
     |   
     |    
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /|\\
     |   
     |   
     |   
     |     
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /|\\
     |    |
     |   
     |   
     |     
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /|\\
     |    |
     |   / 
     |   
     |     
    ----------
    """,
    """
     ------
     |    |
     |    O
     |   /|\\
     |    |
     |   / \\
     |  
    ----------
    """]

#Amount of guesses you get
max_wrong = len(hangman) - 1

#Removes all words that are smaller than 2 and larger than 15
for word in word_list:
   if len(word) > 15 or len(word) < 2:
       word_list.remove(word)


print 'Welcome to Hangman, where you have to guess a word... but watch out! The wary sheriff is out to hang you.'\
     'Complete the word before you run out of guesses.'

#Base
new_word = random.choice(word_list)
so_far = '-' * len(new_word)
wrong = 0
used = []

#As long as you are not dead yet, this code runs
while (wrong < max_wrong) and (so_far != new_word):
   print hangman[wrong]
   print 'You\'ve used the following letters:\n', used
   print 'So far, the word is:\n', so_far

   #Asks for your guess
   guess = raw_input('\n\nEnter your guess: ')
   guess = guess.lower()

   #If the guess was used before
   while guess in used:
       print 'You\'ve already guessed the letter:', guess
       guess = raw_input('Enter your guess: ')
       guess = guess.lower()

   #Appends guess to list of used words
   used.append(guess)

   #If guess is part of the word, the letter would be added
   if guess in new_word:
       print '\nYes!', guess, 'is in the word!'
       
       #Adds the letter to the show portion of the word
       new = ""
       for i in range(len(new_word)):
           if guess == new_word[i]:
               new += guess
               #Doesn't add letter if else statement is active
           else:
               new += so_far[i]
       so_far = new
   #If guess is not part of the word, letter is not added
   else:
       print 'Sorry,', guess, 'isn\'t in the word.\n'
       wrong += 1

#The end of the game, message is determined by whether or not you get the word
def ending():
   if wrong == max_wrong:
       print hangman[wrong]
       print 'You\'ve been hanged!\n'
   else:
       print 'You guessed it!\n'

ending()

#Prints the solution
print 'The word was', new_word

We don't delete posts. Why would you like to delete one?
Reply


Messages In This Thread
fun with hangman! - by Low_Ki_ - Apr-20-2017, 02:03 AM
RE: fun with hangman! - by Turry - Apr-28-2017, 07:14 AM
RE: fun with hangman! - by Low_Ki_ - May-03-2017, 06:10 PM
RE: fun with hangman! - by Neosiq - Jun-12-2017, 01:49 AM
RE: fun with hangman! - by buran - Jun-12-2017, 04:18 PM
RE: fun with hangman! - by Neosiq - Jun-12-2017, 04:04 PM
RE: fun with hangman! - by Neosiq - Jun-12-2017, 04:21 PM
RE: fun with hangman! - by Skaperen - Jun-13-2017, 05:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman Troubleshooting Ewilliam51 5 3,078 Jul-22-2020, 07:09 AM
Last Post: Sooqa
  hangman doesn't end after guessing the word vinci 3 1,903 Nov-06-2019, 09:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020