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.

00:24 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 random.

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.

00:45 Speaking of choices, you’ll want to specify just what the choices are. Line 4 declares CHOICES, a list containing the strings "rock", "paper", and "scissors". Line 6 prints a challenge to the player, and then line 8 requests their response.

01:03 The built-in 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 user_choice.

01:15 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.

01:31 The random module’s 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.

01:48 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 user_choice and computer_choice values are inserted into the string.

02:12 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.

02:37 Here, I’ll run the script, enter rock,

02:43 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 marshmallow.

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.

03:22 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.

03:33 The while statement loops until the condition associated with it is evaluated to False. As True will never evaluate to False, this loop goes on forever.

03:44 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.

04:00 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.

04:08 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.

04:33 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.

04:52 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.