How to replace the underscores with chosen letters in hangman
Question: In my piece of code, I am having trouble replacing the underscores displayed
by the length of the word with the correct letters. How do I solve this?.

Below is a example of my code being run.

    
    
    print("Welcome to Python Hangman")
    print()
    
    import random # Needed to make a random choice
    from turtle import * #Needed to draw line
    
    WORDS= ("variable", "python", "turtle", "string", "loop")
    
    word= random.choice(WORDS)#chooses randomly from the choice of words
    print  ("The word is", len(word), "letters long.")# used to show how many letters are in the random word
    
    space = len(word)
    underscore = ("_ " * space)
    print(underscore)
    
    for i in range(1, 9):#gives the amount of guesses allocated
        letter = input("Guess a letter ")
        if letter in word:
            print ("Correct", letter)#if guesses letter is correct print correct
        else:
            print ("Incorrect", " ",letter)
            #if its wrong print incorecct 
    ​
    


Answer: Sadly, it's not possible to replace the underscores after you printed them to
a console window. Your options are:

  1. to implement a [GUI](https://en.wikipedia.org/wiki/Graphical_user_interface "Graphical User Interface \(Wikipedia\)"), which allows you to change the text after it is displayed
  2. to print a lot of empty lines to make the old version of your output text disappear (which gives the impression that the output has changed)
  3. to print an updated version each time your output text needs to be changed (which is what I would recommend). See my description of this option below.

* * *

For option 3 your output may look like this:

    
    
    Welcome to Python Hangman
    
    The word is 6 letters long.
    
    _ _ _ _ _ _ 
    Guess a letter t
    Correct t
    
    t _ _ t _ _ 
    Guess a letter u
    Correct u
    
    t u _ t _ _ 
    Guess a letter e
    Correct e
    
    t u _ t _ e 
    Guess a letter 
    

To achieve this, you should have a list like this:

    
    
    output = ['_'] * len(word)
    

Now you can replace the spaces in this list each time a user finds a correct
letter by doing:

    
    
    for i,x in enumerate(word):
        if x is letter:
            output[i] = letter
    

and print the updated list using a function:

    
    
    def print_output():
        print ''.join([str(x)+" " for x in output])
    

Which gives you the desired output.

Complete solution:

    
    
    print "Welcome to Python Hangman"
    print
    import random # Needed to make a random choice
    
    WORDS = ("variable", "python", "turtle", "string", "loop")
    
    word = random.choice(WORDS)#chooses randomly from the choice of words
    print "The word is", len(word), "letters long." # used to show how many letters are in the random word
    
    output = ['_'] * len(word)
    
    # function to print the output list
    def print_output():
        print
        print ''.join([x+" " for x in output])
    
    for i in range(1, 9):#gives the amount of guesses allocated
        print_output()
        letter = raw_input("Guess a letter ")
        if letter in word:
            print "Correct", letter #if guesses letter is correct print correct
    
            # now replace the underscores in the output-list with the correctly
            # guessed letters - on the same position the letter is in the 
            # secret word of course
            for i,x in enumerate(word):
                if x is letter:
                    output[i] = letter
    
        else:
            print "Incorrect", " ", letter
            #if its wrong print incorecct 
    

