TY Python Lesson 02, Exercises of Web Programming and Technologies

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

2022/2023

Uploaded on 03/01/2023

shyrman
shyrman 🇺🇸

4.2

(6)

239 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
TY Python
Lesson 02
Rock paper scissors
User input
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.
pf3

Partial preview of the text

Download TY Python Lesson 02 and more Exercises Web Programming and Technologies in PDF only on Docsity!

TY Python

Lesson 02

Rock paper scissors

User input

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.

Random numbers

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.

Converting from a number to a letter

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.