I\'m currently making a hangman program but I am stuck. Whenever I enter a correct guess for the word I keep getting the error \'str\' object does not support item assignment. <
newBoard
is a string and strings are immutable in Python - that's why item assignment is not allowed.
You must build a new string. For example, if you wanted to replace the character at position 4 (the o) in 'helloworld'
, you'd issue
>>> s = 'helloworld'
>>> s = s[:4] + 'X' + s[5:]
>>> s
'hellXworld'
Another option would be to use a bytearray:
>>> s = bytearray('helloworld')
>>> print(s)
helloworld
>>> s[4] = 'X'
>>> print(s)
hellXworld