Stop being the product.
Become the owner.
or
sign uplog in

TypeError: 'str' object does not support item assignment…

TypeError: 'str' object does not support item assignment. In Python, strings (str) are immutable, that is, you cannot directly modify a character at a given position.

In your code, the problematic line is:

python
pen1[pendind%len(pen1)] = de[pendind%len(de)]
Here you are trying to replace a character in pen1 (which is a string), which is not allowed.

Solution
You must convert the string into a list of characters, modify it, then possibly convert it back into a string:

python
pen1 = list('Z' * len(de)) # list of 'Z'
...
if(x in pen): 
print(x, 'complete the hanged man') 
pen1[pendind % len(pen1)] = de[pendind % len(de)] # now it's possible 
calcf += len(from)
...
print('New pen value:', "".join(pen1)) # convert back to str
Explanation
list('Z' * len(de)) creates a list of characters, for example ['Z','Z','Z'].

Lists are mutable, so you can change one element per index.

After modification, we use "".join(pen1) to reconstruct a string.

With this fix, your code should work without raising the error.
earnings
2,000 mlx total
$0  total
engagement
3 views
0 reactions

0 comments