Input and Loops
00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll show you how to get input from users and then use a loop to create repeated play.
00:10 The simplest form of the game starts by taking the user’s choice, having the computer choose a random response, then displaying the result to the user. Let’s see some code to do just that.
I’ve created a file called
simple.py for my first attempt at the game. The first bit of code in here is
import random. This loads the Python module named
00:35 This module contains functions that allow for randomly selecting between choices and numbers. You’ll use it to choose the computer’s response to the player.
Speaking of choices, you’ll want to specify just what the choices are. Line 4 declares
CHOICES, a list containing the strings
"scissors". Line 6 prints a challenge to the player, and then line 8 requests their response.
input() function takes a string that is used to prompt the user and returns whatever the user typed in. The user’s choice will be stored in the variable named
You shouldn’t just accept any random thing from the user. You want to make sure it’s a valid choice. Line 10 verifies that what the user typed in is one of the items in the
CHOICES list. If it is, then line 11 randomly selects the computer’s response.
choice() function takes a list and randomly chooses one of the items in the list. The selection is returned and, in this case, is stored in
computer_choice. Lines 12 through 14 contain a print statement showing the result to the user.
If you haven’t seen an f-string before, it is a way of creating a template that Python will fill in. An f-string is prefixed with the letter
f, hence the name, and then it looks for brace brackets—those are the curly ones—inside the string. Values in the brace brackets are replaced with their corresponding variables. In this case, the
computer_choice values are inserted into the string.
The whole thing is then put to the screen by the
print() function. Since line 10 checked for valid values, you want to tell the user if they didn’t choose something valid. Line 16, inside this
else clause, tells the user what they typed and tells them it wasn’t a good choice.
02:30 Let’s see the game in action.
Here, I’ll run the script, enter
and it tells me that I chose
'rock' and the computer threw
'scissors'. Let me run it one more time. This time, I’ll enter
02:56 Nope, not a valid choice. Each time you want to play the game, rerunning the command is a lot of work. Okay, not that much, but you can still make it easier.
03:07 What about asking the user if they want to throw down again? To do this, you’ll need to wrap all the game logic in a loop, and after the result is shown, prompt the user if they want to play again. Let’s see the code.
I’m inside of
loop.py. This program is a modification of
simple.py, introducing the play-it-again mechanic. Line 6 adds an infinite loop.
while statement loops until the condition associated with it is evaluated to
True will never evaluate to
False, this loop goes on forever.
The next chunk of work is just the code from
simple.py but embedded inside of the loop. Then, after that, at line 17 is a new edition. Once a round has been played, this prompts the user if they want to play again.
Line 18 checks the user’s answer and sees if it matches
"n" for no. The
.lower() method is called on the answer before it is compared.
This method returns the lowercase version of the string it is being called upon. That way, if the user inputs capital
"N", it will be turned into a lowercase
"n" before it is compared. If the user indicates no, then the
break statement is called. The
break statement breaks you out of the surrounding loop. In this case, the code would continue to line 23.
If the user typed anything but
"n", it is assumed they meant yes. In this case, the code inside the loop continues. Line 21 is an empty
print(), which will display a blank line. After that, the execution continues back to the top of the
while loop, asking the user to make their next throw.
And just because it is polite, the code says goodbye to the user after breaking from the
while loop. Let’s see this new code in all its glory.
05:04 Let me run the code. I’m going to enter a choice. This time, I’ll pick paper.
05:12 It shows me what both the computer and I picked and then prompts if I want to go again. I’ll type yes,
05:20 and the game continues. Let’s choose paper again.
05:26 And now I think I’m done. See? Isn’t it nice that it said goodbye? Just printing the choices isn’t good enough. In the next lesson, I’ll show you how to say who won.
Become a Member to join the conversation.