1
print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))

if(words[i]):
    print("The length of the word is: " , len(words[i]))




guesses=0

while guesses<6:
    guess=input("Please enter the letter you guess: ")


    if(guess in words[i]):
        print("The letter is in the word.")


    else:
        print("The letter is not in the word.")
        guesses=guesses+1

    if guesses==6:

        print("Failure. The word was:" , words[i])

Having problems with finding the position of the guessed letter in mystery word. I want a output that shows the correctly guessed letters in the mystery word. Ex. Mystery word is blue. User inputs "b". Output is: "The letter is in the word. Letters matched: b___"

  • so what have you tried? There a lot of ways to do this, for instance two strings SECRET and ??????, guess e and you get S?CR?T and ?E??ET. Thinking about how you want to display the word after a correct guess, would have helped you. – Tony Hopkinson Oct 22 '13 at 21:23
  • possible duplicate of [Hangman program in python issue with position](http://stackoverflow.com/questions/19526715/hangman-program-in-python-issue-with-position) – beroe Oct 22 '13 at 21:59
  • Yes @beroe, that was before I got your comment and recommendation to follow the steps on http://stackoverflow.com/questions/4664850/find-all-occurrences-of-a-substring-in-python/4664889#4664889 No idea how to find an answer by implementing this though :S –  Oct 22 '13 at 22:22

3 Answers3

1

There's many ways to write this, how about if you put all guessed letters in the string called guessed and then compute

''.join(c if c in guessed else '_' for c in words[i])

c will be iterating over the characters of words[i], the word to guess.

The c if c in guessed else '_' bit replaces all characters that have not yet been guessed with underscores.

''.join() will glue the characters back together into a string.

flup
  • 26,937
  • 7
  • 52
  • 74
  • Okay but how exactly do I put all guessed letters into a string called guessed? "guessed= ", something like that? I apologise for all the questions, I am very new to python. –  Oct 22 '13 at 23:27
  • Start it as `guessed = ''` and then do `guessed = guessed + guess[0]`. Or something like that. I do think the stackoverflow format is better served if you ask each question separately (and do a little searching / reading up in between asking the questions :) ) – flup Oct 22 '13 at 23:31
  • Last question I promise, in the program where exactly do I place: guessed='' guessed=guessed+guess[0] ''.join(c if c in guessed else '_' for c in words[i]) I very much want the output to be something similar to if not the same as Please enter a number (0<=number<10) to choose the word in the list: 2 The length of the word is: 4 Please enter the letter you guess: e The letter is in the word. you have guessed these letters: e Letters matched so far __ee –  Oct 22 '13 at 23:42
  • Adding the guessed letter to the guesses happens after the user enters his guess and before you print the current state of the word. The expression computes the __ee. You compute it when you wish to print it. – flup Oct 22 '13 at 23:50
  • So guesses=0 and guessed=guessed+guess[0] goes just below the input in the while loop? Just double checking. –  Oct 23 '13 at 00:03
  • Try it out, print the expression. Toy around with the program till you understand where things go. The fun of coding is that you cannot break anything by trying stuff out. (Well, possibly when toying around with files...) – flup Oct 23 '13 at 00:08
0

I think you're looking for something like this:

word = []
for x in range(len(words[i])):
    word.append('_')

if(guess in words[i]):
        print("The letter is in the word.")
        for index, letter in enumerate(words[i]):
            if letter == guess:
                word[index] = guess

A full program looking like:

print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))

if(words[i]):
    print("The length of the word is: " , len(words[i]))

guesses=0
letters_guessed = []
word = []
for x in range(len(words[i])):
    word.append('_')

while guesses < 6:
    guess=input("Please enter the letter you guess: ")

    if(guess in words[i]):
        print("The letter is in the word.")
        for index, letter in enumerate(words[i]):
            if letter == guess:
                word[index] = guess
        letters_guessed.append(guess)

    else:
        print("The letter is not in the word.")
        guesses=guesses+1
        letters_guessed.append(guess)

    print("you have guessed these letters: %s"%(''.join(letters_guessed)))
    print("Letters matched so far %s"%''.join(word))
    print()

    if ''.join(word) == words[i]:
        break

if guesses==6:
    print("Failure. The word was:" , words[i])
else:
    print("YOU'VE WON!! Great Job!")
    print("You only made %i wrong guesses"%guesses)

which outputs:

>   Welcome to Hangman! Guess the mystery word with less than 6 mistakes!
>   Please enter a number (0<=number<10) to choose the word in the list: 2
>   The length of the word is:  4
>   Please enter the letter you guess: e
>   The letter is in the word.
>   you have guessed these letters: e
>   Letters matched so far __ee
groteworld
  • 701
  • 8
  • 20
0

The code above works, but having only 5 items in the list of words to choose you can have an error if you input a number higher than 5, so I put some other words in the list, but I also deleted the lines with the input function where the user is asked to digit the number of the item related to the word to discover. So: I imported the random module.
I deleted the input code.
I used the random.sample function to store the word to discover (line 12).
I substituted words[i] with samplecode[0] as label for the word to discover.
I added a space after the underscore in word.append('_ ') to make the number of the letters more evident ( _ _ _ _ _ instead of _____ ).

import random

print("Welcome to Hangman! Guess the word in less than 6 try.")

words= ['utopian','fairy','tree','monday','blue',
            'winner','chosen','magician','european',
        'basilar','fonsaken','butter','butterfly',
        'flipper','seaside','meaning','gorgeous',
        'thunder','keyboard','pilgrim','housewife'
        ]

sampleword = random.sample(words,1)

if(sampleword[0]):
    print("The length of the word is: " , len(sampleword[0]))

guesses=0
letters_guessed = []
word = []
for x in range(len(sampleword[0])):
    word.append('_ ')

while guesses < 6:
    guess=input("Please enter the letter you guess: ")

    if(guess in sampleword[0]):
        print("The letter is in the word.")
        for index, letter in enumerate(sampleword[0]):
            if letter == guess:
                word[index] = guess
        letters_guessed.append(guess)

    else:
        print("The letter is not in the word.")
        guesses=guesses+1
        letters_guessed.append(guess)

    print("you have guessed these letters: %s"%(''.join(letters_guessed)))
    print("Letters matched so far %s"%''.join(word))
    print()

    if ''.join(word) == sampleword[0]:
        break

if guesses==6:
    print("Failure. The word was:" , sampleword[0])
else:
    print("YOU'VE WON!! Great Job!")
    print("You only made %i wrong guesses"%guesses)
  • Please edit your answer and provide a description in what way your solution differs from other answers, and, more importantly, why it answers the question better than other answers. – theDmi Aug 10 '15 at 11:59