I was watching a tutorial to make hangman game using Python on YouTube and they used below code in it and didn't explain it.
How does this line work?
word_list = [letter if letter in used_letters else '-' for letter in word]
What does that '-' mean/do in this statement?
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
CodePudding user response:
Your first line contains a list comprehenshion.
It is equivalent to:
word_list = []
for letter in word:
if letter in used_letters:
word_list.append(letter)
else:
word_list.append('-')
The second part:
alphabet = set(['a', 'b', 'c', 'd', 'e'])
used_letters = set(['a', 'b', 'c'])
alphabet - used_letters # produces a set: {'d', 'e'}