I'm really new to python and trying to build a Hangman Game for practice.
I'm using Python 3.6.1
The User can enter a letter and I want to tell him if there is any occurrence of that letter in the word and where it is.
I get the total number of occurrences by using occurrences = currentWord.count(guess)
I have firstLetterIndex = (currentWord.find(guess))
, to get the index.
Now I have the index of the first Letter, but what if the word has this letter multiple times?
I tried secondLetterIndex = (currentWord.find(guess[firstLetterIndex, currentWordlength]))
, but that doesn't work.
Is there a better way to do this? Maybe a build in function i can't find?
Asked
Active
Viewed 1.7k times
2

Piratenlulatsch
- 75
- 1
- 2
- 10
-
1Hint: `find` has an optional argument `start`, which may be useful to you. – Kevin Jun 01 '17 at 12:49
-
3Possible duplicate of [python - find char in string - can I get all indexes?](https://stackoverflow.com/questions/11122291/python-find-char-in-string-can-i-get-all-indexes) – P. Siehr Jun 01 '17 at 12:50
-
@Kevin I know that, I (tried) used it `secondLetterIndex`. I tried to start at the index of the first letter and end at the length of the word. Did not work. – Piratenlulatsch Jun 01 '17 at 12:53
-
If you're saying "I tried the two-argument form when I did `secondLetterIndex = (currentWord.find(guess[firstLetterIndex, currentWordlength]))`", you are not actually using the two argument form there. You are passing a single argument, `guess[firstLetterIndex, currentWordlength]`, to `find`. – Kevin Jun 01 '17 at 13:15
3 Answers
8
One way to do this is to find the indices using list comprehension:
currentWord = "hello"
guess = "l"
occurrences = currentWord.count(guess)
indices = [i for i, a in enumerate(currentWord) if a == guess]
print indices
output:
[2, 3]

Ajax1234
- 69,937
- 8
- 61
- 102
-
2`enumerate()` might be a better option `[i for i, c in enumerate(currentWord) if c == guess]` – AChampion Jun 01 '17 at 12:52
-
Can i call the 2 by itself? Like `indieces[0]` or something similar? – Piratenlulatsch Jun 01 '17 at 12:59
-
Yes, you could do that, if you want just the first element of the indices. – Ajax1234 Jun 01 '17 at 13:00
0
I would maintain a second list of Booleans indicating which letters have been correctly matched.
>>> word_to_guess = "thicket"
>>> matched = [False for c in word_to_guess]
>>> for guess in "te":
... matched = [m or (guess == c) for m, c in zip(matched, word_to_guess)]
... print(list(zip(matched, word_to_guess)))
...
[(True, 't'), (False, 'h'), (False, 'i'), (False, 'c'), (False, 'k'), (False, 'e'), (True, 't')]
[(True, 't'), (False, 'h'), (False, 'i'), (False, 'c'), (False, 'k'), (True, 'e'), (True, 't')]

chepner
- 497,756
- 71
- 530
- 681
-
1you can create matched as `matched = [False] * len(word_to_guess)` and avoid the loop – Jun 01 '17 at 13:05
-
That's bad practice; it only works as intended for immutable values. It's what leads to countless questions like "What's wrong with my code that sets `x = [[]]*5`?". – chepner Jun 01 '17 at 13:13
0
def findall(sub, s) :
pos = -1
hits=[]
while (pos := s.find(sub,pos+1)) > -1 :
hits.append(pos)
return hits

Stephen Boston
- 971
- 1
- 12
- 23
-
Please consider adding some explanation to the source code explaining how it solves the problem. – Azhar Khan Nov 17 '22 at 11:08