This happens because both secretWord
and compareWord
point to the same list.
The first line of code creates a new list from the string Apple, and assigns that list to secretWord
. The second line assigns this list to compareWord
- both variables now point to the same list! The for-loop changes each item in the list to an underscore. When printing this list it rightly shows 5 underscores.
If you move the print("".join(secretWord))
call to after the loop, you will see that the output of that call has also changed to the 5 underscores.
A simple way to fix this is to assign a copy of the list to compareWord
(that is, a new list but with the same contents of the old one). For exmaple:
- Slicing:
compareWord = secretWord[:]
- New list call:
compareWord = List(secretWord)
For some detailed information on how to clone a list in Python, see How to clone or copy a list?
For some intro on Python and lists, see https://developers.google.com/edu/python/lists, this also nicely shows the problem your encountering.