0

I have a problem with contiunation of the loop. Here is a code of the hangman for Python. Although using the list or just print for each character seem to be easier ways to proceed the task, I would like to know, where is the problem of the presented solution in order to avoid the future troubles.

word='heksakosjokontaheksafobia'

def hangman(input, word_to_guess):
    users_guess=input('What is the first letter of your first thought?\n')
    print (users_guess)
    if users_guess in word_to_guess:
        unknown_chararcters='*'*len(word_to_guess) # The creation of a string showing number of uknown characters
        print(unknown_characters)
        for char in word_to_guess:
            if char==users_guess: # Checking whether input is included in word_to_guess
                ind=word_to_guess.index(char)
                unknown_characters=unknown_characters[:ind]+users_guess+unknown_characters[ind+1:] # Substitution of '*' with input
            continue
        print(unknown_characters)
hangman(input, word)

As you can see, the word includes many 'a', however for input==a the output is:

What is the first letter of your first thought?
a
*************************
****a********************

How can I force the loop to iterate over the following characters of a string?

Barmar
  • 741,623
  • 53
  • 500
  • 612
fgh
  • 169
  • 1
  • 3
  • 15
  • 2
    do not use python keywords for variable names. `str` and `input` i can see at a glance. – Paritosh Singh Dec 12 '18 at 16:31
  • The first thing I would advise is to go back and rename your variables, parameters, and arguments so that you aren't using reserved words like `str` and `input` that could shadow existing objects or methods. Then name your variables meaningfully, rather than just `r` or `h` or `l`, as this will help you and others read your code more easily – G. Anderson Dec 12 '18 at 16:33
  • 2
    `str.index(x)` will return the index of the first occurrence of `x`. Instead, you should iterate over `for index, item in enumerate(str):` – Patrick Haugh Dec 12 '18 at 16:33
  • Brilliant! Thank you. – fgh Dec 12 '18 at 16:41
  • @DanielGL, could you direct me to the case with the presented usage of your mentioned 'while' loop for hangman? – fgh Dec 12 '18 at 16:44

0 Answers0