Python

a_giunta67

Baseband Member
Messages
37
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)
 
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!
 
Back
Top Bottom