-2
while turn < 10:
    letter = str(input("Enter your guess: "))
    
    if letter in randomword:
        
        pos = randomword.find(letter)
        
        word = word.replace(word[pos],letter,1)
        print(word)

word is a string like this "_ _ _ _ ". Why, in this loop, when the letter is in the random word, it gets printed in the beginning and not in the position word[pos]?

martineau
  • 119,623
  • 25
  • 170
  • 301
Giorgos Marga
  • 15
  • 1
  • 6
  • https://idownvotedbecau.se/nomcve/ – Jab Aug 25 '20 at 19:02
  • `str.replace(find, repl, 1)` replaces the _first instance of `find`_ with `repl`. In the beginning, `word[pos]` is always `_`. What do you think the first instance of `_` is in your word? [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Pranav Hosangadi Aug 25 '20 at 19:02
  • It looks like you haven't provided a specific example of what you're seeing. Could you provide a specific example of what's happening, and what you expect to happen? – Steven Rouk Aug 25 '20 at 19:02
  • @StevenRouk its like a hangman game the var word is a string liek this "_ _ _ " .These represent the word that the use has to find.So when he inpus the letter the code tries to find out if the letter is in the random word and if it is it founds the specific index of it.Then it is supposed to replace that index in the word variable with the letter that the user entered.For example if the randomword = "qwe" then word = "_ _ _" and then if te user inputs the letter w it is supposed to print " _ w _" – Giorgos Marga Aug 25 '20 at 19:06

2 Answers2

1

The first argument replace takes is the substring to be replaced, in the case of this code that is whichever character is currently at word[pos]. Then, it will replace the first occurrence of that character, which will be at the beginning of the string. You would have to use word[pos] = letter to change the character at pos.

Edit: sorry, I thought this was possible while it isn't. What does work, and is faster than converting to a list of characters, is using string slicing. In that case, the code would be word = word[:pos] + letter + word[pos+1:]

By the way, this has already been answered here

melbok
  • 95
  • 7
  • Thats what i tried at the begging but it gives me an error – Giorgos Marga Aug 25 '20 at 19:08
  • word[pos] = letter TypeError: 'str' object does not support item assignment – Giorgos Marga Aug 25 '20 at 19:09
  • @GiorgosMarga: Python strings are [immutable](https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str). Therefore you first need convert it to a list of characters, change the character there and convert it back to a string. See my answer ;-) – Franz Diebold Aug 25 '20 at 19:12
  • @melbok: You cannot do that in Python: `word[pos] = letter`, since Python strings are immutable. – Franz Diebold Aug 25 '20 at 19:13
0

Python strings are immutable. Therefore you first need convert it to a list of characters, change the character there and convert it back to a string:

turn = 0
randomword = 'Testword'
word = ''.join('_'*len(randomword))
while turn < 10:
    letter = str(input("Enter your guess: "))
    if letter in randomword:
        pos = randomword.find(letter)
        char_list = list(word)
        char_list[pos] = letter
        word = ''.join(char_list)
        print(word)
Franz Diebold
  • 555
  • 6
  • 21