-4

I am writing code for a hangman game in python and I need to know the index of some of the letters which are in a list.

Here is my code:

if answer in word:
    check.append(answer)
    if check[0]==answer:
        print('u guessed one')
        guessed.append(answer)
        check.remove(check[0])
        print('You have guessed',guessed)

I know that I have to find the position of the guessed letter but I don't know how..

Can someone help me?

andrewJ
  • 111
  • 2
  • 11
  • 2
    Possible duplicate of [Finding the index of an item given a list containing it in Python](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – alkasm Jun 18 '17 at 20:37
  • What variable do you have to find the position of in what array? – Noah Cristino Jun 18 '17 at 20:39
  • If it is not too taxing for you: google for 'python string index' and hit the first link proposed, read an apply. If you don't want to use the string method described there but walk over the elements yourself, look-up `python enumerate`. And you can of course do `idx =0` and then `idx += 1` in the loop, if you really have no clue – Anthon Jun 18 '17 at 20:43

1 Answers1

0

I can't tell what you want to find the position of (sloppy variable naming) but I think you are trying to find the position of check[0] in word. To do this you use index, a built in function:

position = word.index(check[0])

If word = "cake", and check[0] = a then, the position would be 1, since a has an index of 1 in word (['c', 'a', 'k', 'e']).

Noah Cristino
  • 757
  • 8
  • 29