Python Forum

Full Version: fun with hangman!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the middle of creating a text-base hangman game, something came up and I wont be able to finish if for a while but I have a basic version down if anyone is interested in helping that would be great! I'd like to change difficulty, add categories to choose from, also I need to re-organize the code and create some classes that way I can keep the main() code separate from the modules needed for the game to run.

Here is the git link:

https://github.com/Low-Ki/CreateYourOwnC...rGames.git



There are a few projects in that repo that I am working on, but just look through and you'll find the hangman.py file.
(Apr-28-2017, 07:14 AM)Turry Wrote: [ -> ]https://github.com/Turry99/Python-script...ngedman.py
I've done one similar a bit ago.

:) Nice!
#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
How do I remove a post?
(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?
How do we fix a post?
you can edit your post if you have an edit button.