Duplicate item in a list

If letter is o the code keeps overwriting at index 1.
How do i tell it to skip if same item already added to outputList

Repl link: https://replit.com/@maditsilekota/Day39100Days

wordChosen = koloi 

outputList = []
for i in wordChosen:
  outputList.append("_")

while missed < 6:
  letter = input("Type in a letter> ")
  print()
  if letter in wordChosen:
    index = wordChosen.index(letter)
    outputList[index] = letter
    print(outputList)
    missed += 1
1 Like

Welcome to the forums, @maditsilekota!
If I understand you correctly, this is how your code should look:

wordChosen = koloi 

outputList = []
for i in wordChosen:
  if not(i in outputList):
    outputList.append("_")

while missed < 6:
  letter = input("Type in a letter> ")
  print()
  if letter in wordChosen:
    index = wordChosen.index(letter)
    outputList[index] = letter
    print(outputList)
    missed += 1

If this doesn’t do what you need, just reply to this question and I’ll be happy to help! :slight_smile:

1 Like

You can add a check to see if the letter has already been guessed in the outputList . If it has, you can skip it.

wordChosen = "koloi"
outputList = ["_"] * len(wordChosen)  # Initialize the list with underscores

missed = 0

while missed < 6:
    letter = input("Type in a letter> ")
    print()

    if letter in wordChosen:
        indices = [i for i, char in enumerate(wordChosen) if char == letter]

        for index in indices:
            if outputList[index] != letter:
                outputList[index] = letter

        print(outputList)
    else:
        missed += 1

print("end.")

Thank you very much. Code is working perfect… :ok_hand:

Hi, @maditsilekota!
If one of the two above posts helped, please mark it as the solution so that people with similar issues can find a possible answer.

1 Like