Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-04-2010, 01:22 PM   #1
Baseband Member
 
Join Date: Dec 2009
Posts: 37
Default Python

For my Python class, I have to write a hangman program. I'm not finished with all of it, but the problem I'm having is getting the program to display the letters after the person guesses them, like this:
--E--A---E--O--
Instead, if a person chooses A, it will show as:
-----A--------
And then if the person choses O, it will show as:
--------O--
Code:
import random

#FUNCTIONS
def select_word():
    WORDS = ("COMPUTERS", "ARCHITECTURE", "CONSTRUCTION", "ENGINEERING", "ELECTROMECHANICAL", "MANAGEMENT", "INTERIOR", "NETWORKING", "WENTWORTH", "DESIGN")
    word = ""
    word = random.choice(WORDS)

    return word

def create_hangman():
    
        HANGMAN = (
        """
         ------
         |    |
         |
         |
         |
         |
         |
         |
         |
        ----------
        """,
        """
         ------
         |    |
         |    O
         |
         |
         |
         |
         |
         |
        ----------
        """,
        """
         ------
         |    |
         |    O
         |   -+-
         | 
         |   
         |   
         |   
         |   
        ----------
        """,
        """
         ------
         |    |
         |    O
         |  /-+-
         |   
         |   
         |   
         |   
         |   
        ----------
        """,
        """
         ------
         |    |
         |    O
         |  /-+-\
         |   
         |   
         |   
         |   
         |   
        ----------
        """,
        """
         ------
         |    |
         |    O
         |  /-+-\
         |    |
         |   
         |   
         |   
         |   
        ----------
        """,
        """
         ------
         |    |
         |    O
         |  /-+-\
         |    |
         |    |
         |   | 
         |   | 
         |   
        ----------
        """,
        """
         ------
         |    |
         |    O
         |  /-+-\
         |    |
         |    |
         |   | |
         |   | |
         |  
        ----------
        """)
        return HANGMAN

def output_message(word_is_guessed, word):
    if word_is_guessed:

        print "The word is", word, "\nCongradulations!  You guessed the word!"
    else:
        print "The word is", word, "\nSorry, you didn't get the word"

def check_if_used(letter, used_letters):
    while letter in used_letters:
        print "You've already used the letter", letter
        letter = raw_input("Enter your guess: ")
        letter = letter.upper()
    else:   
        used_letters.append(letter)
    return letter


def insert_letter(letter, dashed_word, word):
    new=""
    for i in range(len(word)):
        if letter == word[i]:
            new = new + letter
        else:
            new = new + dashed_word[i]              
            
    return new
    



def guess_letter(word, HANGMAN, wrong_guesses, dashed_word):
    MAX = len(HANGMAN)-1
    
    
    while dashed_word != word and wrong_guesses<MAX:
        
        letter = raw_input("Guess a letter: ")
        letter = letter.upper()
        
        check_if_used(letter, used_letters)
        if letter in word:
            print letter, "is in the word!"
            dashed_word=insert_letter(letter, dashed_word, word)
            
        else:
            wrong_guesses = wrong_guesses + 1
        print "The word:", dashed_word
        print "You have used the letters:", used_letters
        
        
        return dashed_word
    

print \
      """
        Welcome to hangman!
     """
HANGMAN = create_hangman()
used_letters = []
wrong_guesses = 0
word = select_word()
dashed_word = "-"*len(word)
MAX = len(HANGMAN)-1

for i in range (MAX):
        guess_letter(word,HANGMAN,wrong_guesses, dashed_word)
__________________

a_giunta67 is offline   Reply With Quote
Old 12-29-2010, 05:03 PM   #2
Jsy
Baseband Member
 
Join Date: Dec 2005
Posts: 91
Default Re: Python

Since no one has replied, I'd redirect you to a different website.

www.python-forum.org &bull; Index page

I got a response within 10 minutes with results.
__________________

__________________
3800x AMD, ASUS-E mobo, four 512 sticks of ddr400 (duel channels), Nvidia 7900gt OC, 80gig, 120gig, 200gig, 320gig, 400gig WDs, and a Soprano case
Jsy is offline   Reply With Quote
Old 12-30-2010, 06:27 AM   #3
j03
~~~~~~~~
 
Join Date: Jan 2005
Location: Wales
Posts: 5,556
Send a message via MSN to j03
Default Re: Python

This is an old thread, but if I was going to try and do this, I might have each individual letter of the word in an array, and then a separate array (of the same size) which contains the _'s. When someone tries a letter, the program would try and find that letter within the word array, and if it could be found, then it would replace the corresponding element of the _ array with the actual letter... if that makes sense!

Python isn't my... strongest language, but that general idea should work!
j03 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 03:51 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0