Check Letters With Sets
00:00 Check Letters With Sets. So far, you’ve only told the user whether they’ve guessed the entire word correctly or not. To give some hints to allow them to deduce the secret word, you’ll add feedback about the individual letters they guess.
00:15 You’ll classify each letter as belonging to one of three categories seen on-screen. A correct letter appears in the same position in the guess as in the secret word, a misplaced letter appears in the secret word but in a different position, and a wrong letter doesn’t appear in the secret word at all.
00:34 On-screen, you can see some guesses classified when the secret word is SNAKE. But how can you find out which letters belong in each category? You’d start by figuring out which letters are correctly placed.
00:47
Python’s zip()
function is great for doing element-by-element comparisons of two sequences. In this case, you are comparing the letters of two strings.
01:04
In this piece of code, you find the two correctly placed letters in CRANE by comparing SNAKE
and CRANE
one letter at a time.
01:14
Even though there’s an N
in both words, it’s not reported since it’s in different positions. Now, you need to collect the letters and not just print them out.
01:24 Comprehensions are powerful constructions in Python that you use to transform one or more sequences into another. Here, you’ll use a set comprehension to collect the correct letters.
01:41 A set comprehension is similar to a list comprehension but outputs a set instead of a list.
01:50 It works well in this situation because the order of the correct letters isn’t important. One advantage of sets is that Python offers powerful operations on them.
02:01
You can quickly use unions, intersections, and differences between two sets to find elements that appear in at least one set, both sets, or only one of the sets. For example, if you have two strings, then you can use set intersection (&
) to find all the letters that appear in both.
02:22
The intersection tells you that A
, E
, and N
are both in SNAKE
and CRANE
. Similarly, you can use the set difference to find letters that appear in one set and not the other.
02:37
C
and R
are the letters in CRANE
that don’t appear in SNAKE
. Note that the intersection (&
), union (|
), and difference (-
) operators in sets are used to implement set-theoretic operations in Python, and these are different from Boolean logic operators, such as and
, or
, and not
. It’s time to use sets to improve the game.
03:00
However, before you start the implementation, you’ll make one other change. Currently, you’ve hard-coded the secret word into the if
test. You’ll use that word when you classify letters, so instead you’ll refer to it using a constant.
03:25
Introducing the WORD
constant makes it easier to change the secret word. In the next section, you’ll add a word list that you’ll choose the word from, making the game more interesting. Using what you’ve explored previously about sets, you can now calculate and display the correct, misplaced, and wrong letters.
03:43 Update the code as seen on-screen.
03:58 You use the set comprehension to find all the correctly placed letters. The misplaced letters are the ones that are in both the guess and the secret word but aren’t correctly placed. Finally, the letters in the guess that aren’t in the secret word at all are categorized as wrong.
04:42 For now, as seen on-screen, you are only listing the categories and the letters.
04:51 While the information is there, it’s not so easy to understand. Later on in the course, you’ll improve the user interface and make the game both nicer to look at and nicer to play. But before that, you need to improve the game’s variety by creating a word list.
Become a Member to join the conversation.