1

So I'm making a hanging man game and I have run into a problem regarding indexes. Basically, I want to find the index of a letter inside a secret word, the problem is that if the secret word includes two letters that are the same, for instance, "guacamole", where the letter a has the index of 2 and 4 but when I want to find the index of a, it only prints "2" and not "4". Is there a way around this? Thanks in advance!

Part of code where problem occurs:

for letter in secret_word:
    if user_guess == letter:
        current_word_index = secret_word.find(letter)
        print(current_word_index) #Not in full program, only to test errors.

Full code:

#Hanging man

import string

space = "\v"

dbl_space = "\n"


secret_word = str(input("Enter a secret word: "))

guess_low = list(string.ascii_lowercase)

used_letters = []


user_errors = 0

user_errors_max = 1

secret_word_index = int(len(secret_word))

secret_word_placeholder = list(range(secret_word_index))




while user_errors != user_errors_max:
    user_guess = str(input("Enter a letter: "))
    if len(user_guess) != 1:
        print("You have to pick one letter")
    if user_guess in guess_low:
        guess_low.remove(user_guess)
        used_letters.extend(user_guess)
        print(used_letters)
    for letter in secret_word:
        if user_guess == letter:
            current_word_index = secret_word.find(letter)

if user_errors == user_errors_max:
    print("You lost the game, the secret word was: " + secret_word)
Philip_99
  • 15
  • 3
  • Does this answer your question? [How to find all occurrences of a substring?](https://stackoverflow.com/questions/4664850/how-to-find-all-occurrences-of-a-substring) – Shinra tensei Feb 18 '20 at 11:29

4 Answers4

1

This is an example of what you are trying to achieve. use list comprehension.

string='hello'
letter='l'
[idx for idx,ch in enumerate(string) if ch==letter]
Ahmad Farhan
  • 575
  • 4
  • 12
0

Python's string find accepts a start parameter that tells it where to start searching:

>>> "guacamole".find('a')
2
>>> "guacamole".find('a', 3)
4

Use a loop, and use the index of the last hit you found + 1 as the start parameter for the next call.

Roy Tang
  • 5,643
  • 9
  • 44
  • 74
0

Another more verbose solution might be:

str1 = "ooottat"
def find_all_indices(text, letter):
    indices_of_letter = []
    for i, ch in enumerate(text):

        if ch == letter:
            indices_of_letter.append(i)
    return indices_of_letter
print(find_all_indices(str1, 'o'))

Side note:

Indexes is the nontechnical plural of index. the right technical plural for index is indices

Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
0

Yes, if you instantiate a new_variable to be the secret_word variable before the for loop, the in the line current_word_index = secret_word.find(letter) change secret_word.find(letter) to new_variable .find(letter) then below the if statement write new_variable = new_variable [1:] which will remove the letter just found.

so your code would look something like:

new_variable  =  secret_word

for i in secret_word
     if user_guess == letter:
            current_word_index = new_variable.find(letter)
               #I would do print(current_word_index) this will show the index
               #Or current_word_index = current_word_index + ',' + new_variable.find(letter)    
     new_variable=  new_variable[1:] #this will take away your letter.
tazs
  • 1
  • 1