How to Determine a Winner
00:11 Just showing the choice the user made and the random computer choice feels a bit lightweight. Let’s add some code to say who won. You’ll need to encode the logic for each situation of X beats Y, where rock smashes scissors, paper covers rock, and scissors cut paper.
The first is named
user_choice and the second,
computer_choice. Line 7 checks if the user and the computer chose the same value. If so, line 8 prints that there was a tie. Line 9 is an else-if, which will only run if the user and the computer chose different values.
Then you do it again for
"scissors" and its associated win and lose conditions. Finally, if anything but
"scissors" was typed, it is caught by the
else clause on line 24. In this case, the user is told their throw was bad.
02:53 A large if-then-else block like I just demonstrated can be a bit hard to read. It only gets worse the more conditions you have to compare. This kind of code is so common that Python 3.10, coming in the fall of 2021, has a new keyword structure called pattern matching.
03:10 This is a new bit of syntax in Python that displays a series of choices based on a pattern. This structure will allow you to write large if-then-else blocks with fewer lines of code. If you’re used to switch statements in other languages, it’s a cousin of that.
beats.py. “Beats” as in “rock beats scissors,” not as in “Womphf, womphf, womphf, womphf.” The first change from the previous program is the introduction of the
BEATS dict. This dictionary is storing the what-beats-what conditions. Each key of the dictionary is paired against a list of things it beats. You’ll understand why I used a list in the next lesson.
04:19 You could simplify the message to the user by changing the language to say “X beats Y” instead of having the specifics of “rock smashes scissors,” but instead, I’m going to encode the verb describing the winning action inside another dict.
Each key in this dictionary is a tuple describing what beats what. The first one says that when rock beats scissors, use the word
"smashes". This could be simplified by just having the winning item be the key to this dictionary, but the next lesson adds some complexity that this prepares for.
But then, the chunk of the if-then-else has changed. Previously, the check for bad data was done at the end of the function. I’ve moved it up here. The
.keys() method on the
BEATS dict returns a list of all the valid keys.
Line 25 does a Boolean comparison checking if the computer’s choice is in the list of things that are beat by the user’s choice. If the user chose
"rock", the key
"rock" is applied to
BEATS, returning the list containing just
"scissors". If the computer chose
"scissors", then there is a match and the resulting Boolean comparison is
This value is stored in
user_wins. Line 27 then evaluates the Boolean called
user_wins. If the user did win, then look up the appropriate verb in the
MESSAGES dict, using the user and computer choice tuple as the key. For the choices of
"scissors", the result will be
"smashes". With the verb ready to go, all that is left is to tell the user the good news.
An f-string allows you to call methods on string objects. The
.capitalize() method returns a new string with the first letter capitalized. This will make the beginning of the sentence look right.
Of course, the user doesn’t always win. The
else clause does the mirror logic. As the computer has won, the verb is looked up using the reverse of the tuple in the previous block, then the user is told the bad news. This code is a bit shorter than the large if-then-else block from
loop.py. It might not even be worth it in this case, but when the game gets more complicated, you’ll have saved a lot of work.
Become a Member to join the conversation.