Operation Manual

Experiments in Python
87
Notes:
This is a much longer program, which keeps asking for a new guess while the
guess doesn’t match the secret. The first thing it does is to randomly select a
sequence of four letters from a choice of six. It builds this list sequence, which is
denoted by “[” and “]”, by looping four times and randomly choosing a letter from
the options each time.
The computer gets the user to guess by waiting for input from them. Until the user
types in something and presses enter the program will stop. The first thing it does
with the input is to convert it to upper case (capital letters) and check it has at
least four letters. The computer now needs to compare the secret answer with the
guess. Next, it converts the sequence of letters from the input function to a list of
letters, the same as the secret.
The next bit of Python magic is “zip”. It takes the two lists and makes a new list,
where each entry has two parts; a left part and a right part. This can be used to
compare the left and right parts in a moment.
Now your program builds yet another list of letters that were correct; that is the
left part and right part were the same in the comparing list. It creates the list by
filling it with “speg” entries. “speg” means “secret peg”, and “gpeg” means “guess
peg”. The program gets the “speg” from the comparing list along with the “gpeg”,
but it only uses the “speg” if it matches the “gpeg.
Correct Letters
= number in correctList
= 2.
Unused Letters
= fewest - correct
= 3 - 2
= 1