0

So I just started experimenting with python and I've learned a few commands with which Im trying to create a hangman-game. If the player finds a letter for the 1st time the program works fine and displays the missing letters as '_', showing the letter in the correct spot. However if the player finds a 2nd letter, the word will just reset and show up just the 2nd letter in its potisionand the missing letters as ' _'. Is there a command that can help me deal with this. Ive used this code for that section

word = input('Give word:\n')
word = word[0].upper()+word[1:]
wordC =word[1:-1]
wordUkn = ' _ '*len(wordC)


while '_' in wordUkn and mist < 3 :    
    ans=input('Give a letter:\n')
    if ans in wordC :
          pos=wordC.index(ans)
          wordUkn=(' _ '*pos)+str(ans)+' _ '*(len(wordC)-(pos+1))
          print(word[0]+wordUkn+word[-1])

mist = mistakes ans=answer

Also If Im new here so If there is anything I should be doing differently about posting questions please inform me. I would also aprreciate it,If anyone could tell me how to deal with the case that the world contains the same letter 2 times. Thanks for your time. :)

  • 1
    Related: https://stackoverflow.com/questions/11122291/python-find-char-in-string-can-i-get-all-indexes – jarmod Oct 13 '17 at 22:04

1 Answers1

0

I understand you want to fill up wordUkn variable, as the user finds each letter.

In order to replace a character in string in Python there is a simpler way: You can change the string to a list, change the character in the list index you already have and then transform the list back to a string, a small example is here:

pos = 5
s = 'good dye'
li = list(s)
li[pos] = 'b'
s = "".join(li)

If you replace 'b' with the current found letter you are done!

agelakis
  • 53
  • 4