-1

I'm making an elementary version of hangman and wish for the computer to tell the user where the correctly guessed letter is ("first", "second", etc.). Here is what I have so far:

print("ok, I have just thought up a word.  It has ",word_count," letters in it.  Make sure you draw that many blank spaces.")
first_try=input("Ok, whats the first letter you want to guess?")
if first_try in mystery_word:
    print("Good guess it is the"    "letter(s) in the word")
Lyndon Gingerich
  • 604
  • 7
  • 17
Andrew
  • 29
  • 6

1 Answers1

0
  • First use a list comprehension to get all the indexes in the mystery_word of the letter for first_try also add 1 since indexes start from 0
  • then use the humanize module all the indexes to the ordinal version (e.g. from 2 to 2nd)
  • Then just use .join to print the ordinals in a nice format along with the Good guess it is ..."
import humanize


mystery_word = "saample"
word_count = 5
print("ok, I have just thought up a word.  It has ",word_count," letters in it.  Make sure you draw that many blank spaces.")
first_try=input("Ok, whats the first letter you want to guess?")
if first_try in mystery_word:
    indexes = [(n+1) for n,x in enumerate(mystery_word) if x == first_try]

    indexes = [humanize.ordinal(index) for index in indexes]

    print("Good guess it is the",','.join(indexes),"letter(s) in the word")

Output:

ok, I have just thought up a word.  It has  5  letters in it.  Make sure you draw that many blank spaces.
Ok, whats the first letter you want to guess?a
Good guess it is the 2nd,3rd letter(s) in the word
coderoftheday
  • 1,987
  • 4
  • 7
  • 21