

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
TY Python. Lesson 02. Rock paper ... The first step to creating a rock paper scissors game is to ask the user to choose either rock, paper or scissors.
Typology: Exercises
1 / 3
This page cannot be seen from the preview
Don't miss anything!


The first step to creating a rock paper scissors game is to ask the user to choose either rock, paper or scissors. To do this we need to use the input function. In the last lesson we wrote the line of code below to ask the user to enter a number and then store it in a variable called first_number. We need to write a line of code very similar to this, but we will need to choose a new variable name, player , and we want to store a letter. This means we don’t need to convert the input to a number and can get rid of the int function. Next, we need to print out the choice entered by the player to confirm that it is stored.
The second step of the project is to simulate the computer choosing a random option. To do this we will use a random number generator to see which option the computer will choose. To generate a random number we will use the randint function. This will give us a random number of 1, 2 or 3. Before we can use this function we will need to import the library that it is contained in. At the top of our code write the following line. This will allow us to use the function. Now we can generate the random and store it in a new variable.
The third step of the project is to convert the random number into either ‘r’, ‘p’ or ‘s’. To do this we will use an if, elif and else statement. The code above first checks if the random number is 1. If it is 1 then the computer variable is set to ‘r’. Only if the number isn’t 1 do we check the elif statement. Finally if the number is also not 2 then we run the else statement. The else statement will always run if neither of the first options are true. We now want to print out what the computer chose.