Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fun with hangman!
#1
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.
Reply
#2
https://github.com/Turry99/Python-script...ngedman.py
I've done one similar a bit ago.
Reply
#3
(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!
Reply
#4
#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
Reply
#5
How do I remove a post?
Reply
#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
#7
How do we fix a post?
Reply
#8
you can edit your post if you have an edit button.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hangman Troubleshooting Ewilliam51 5 2,999 Jul-22-2020, 07:09 AM
Last Post: Sooqa
  hangman doesn't end after guessing the word vinci 3 1,808 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