How to reveal a specific letter in a string for Python?

Viewed 1500

I am in high school and am currently teaching myself Python. I am programming Hangman and it is going well so far (it is not very organized yet) but I am having a problem. I created a list of words that I would use for the game and replaced them with asterisks using the range function. Here is the code for that:

words = ['utopia', 'prosecute', 'delirious', 'superficial', 'fowl', 'abhorrent', 'divergent', 
         'noxious', 'scarce','lavish', 'hinder', 'onerous', 'colossal', 'infringe']

picked_words = random.choice(words)

for i in range(len(picked_words)):
    print(picked_words.replace(picked_words,'*'), end = '')

I am at the point where I am guessing the letters and I need to slowly reveal the position of certain letters. I have tried looking for certain functions (I even thought that the find() function would work since it prints the location of a letter in a string but it did not) and I am at a loss. Especially for words with multiples of the same letter such as "onerous" and "infringe." I will write the code that I have so far for this section:

def right_letter(correct):
   if correct == True:
       print("There are {} {}'s.".format(number_of_char, guess))
       for x in range(len(picked_words)):
           print(picked_words.replace(picked_words,guess, #find way to fill in specific letters),end = '')

I have no clue what parts of the code are necessary to get an answer but my code is really unorganized because I do not know proper techniques yet. The portion that says for x in range(len(picked_words))... is where I am having trouble. The rest of the code ran fine before I added the print statement underneath it. Basically I am asking how to change a certain character or specific characters and put them where they belong. For example, if the randomized word was "infringe" and the first guess was "i" how do I go from ******** to i***i***?

Also, in case anyone was wondering what I originally thought the solution was...

picked_words[(picked_words.find(guess))]

I thought that it would simplify and become picked_words[index]

2 Answers

Here's an example of converting your code to the hangman game.

Note how variable display in the code is updated to show user the correct guessed letters.

import random

def hangman():
    words = ['utopia', 'prosecute', 'delirious', 'superficial', 'fowl', 'abhorrent', 'divergent', 
'noxious', 'scarce','lavish', 'hinder', 'onerous', 'colossal', 'infringe']

    max_guesses = 5
    word = random.choice(words)
    guesses = 0
    display = "*" * len(word)
    print(display, " Is the word")
    while guesses < max_guesses:
        letter = input("Please Enter Your guess letter: ")
        if letter.lower() not in word:
            guesses += 1
            print("You got it incorrect, you have: ", max_guesses - guesses , " Additional    trials")
        else:
            print("Right guess!")
            new_display = ''
            for w, l in zip(word, display):
                if letter.lower() == w:
                    new_display += letter.lower()
                else:
                    new_display += l
            display = new_display
            print(display)
            if not '*' in display:
                print("You won!")
                return

hangman()

Basically you will need to find a partial match between your letter and the word here. The easiest way i can think of is Pythons in Operator. It can be used like this

if picked_letter in picked words

% here you need to figure out at which position(s) the letter in your word occurs 
% and then replace the * at the correct positions. A nice way to do this is 
% the use of regular expressions 

picked_words = re.sub('[^[^picked_letter]*$]', '*', picked_words)

%this will replace every letter in your word with the Asterisk except for the one picked


else

%do Nothing

Note that running this Code in a loop would replace every character in your string except the picked one EVERY time., therefore it is probably best to design your picked_letter as a list containing all characters that have been picked so far. This also makes it easier to check wether a letter has already been entered.

Related