Search code examples
pythonlistsetgoogle-colaboratory

Python if statement not working with two conditions


I was trying to write a code in python but getting error.

use_letter=[]
word = "admit"
guess = 'b'
if guess in word and not in use_letter:
  use_letter.append(guess)

Here I create a list use_letter. I wanted to append guess if the letter already in word but not in use_letter. However, I got error if guess in word and not in use_letter: in this line.

Error message: invalid syntax

I also want to make the use_letter list, a unique element list, where no duplicate elements will be allowed.

How do I create a unique list?


Solution

  • You need to add guess each time you create a new condition. Try this:

    if guess in word and guess not in use_letter:
      use_letter.append(guess)
      use_letter = list(dict.fromkeys(use_letter)))
    

    The last line removes any duplicates.