- 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