0% found this document useful (0 votes)
1K views

Your First Python Game - Rock, Paper, Scissors - Python - The Hello World Program PDF

The document describes how to code a simple Rock, Paper, Scissors game in Python using lists, random number generation, and if/elif/else conditional statements. It creates a list of possible plays, randomly assigns the computer a play, prompts the user for a play, and compares the plays to determine a winner or tie using nested conditional logic. It repeats the process in a while loop so the game continues until the user exits.

Uploaded by

EviVardaki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Your First Python Game - Rock, Paper, Scissors - Python - The Hello World Program PDF

The document describes how to code a simple Rock, Paper, Scissors game in Python using lists, random number generation, and if/elif/else conditional statements. It creates a list of possible plays, randomly assigns the computer a play, prompts the user for a play, and compares the plays to determine a winner or tie using nested conditional logic. It repeats the process in a while loop so the game continues until the user exits.

Uploaded by

EviVardaki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Your First Python Game: Rock, Paper, Scissors

Open IDLE and create a new file, rps.py. Then enter the following:

Python
1 from random import randint
2  
3 #create a list of play options
4 t = ["Rock", "Paper", "Scissors"]
5  
6 #assign a random play to the computer
7 computer = t[randint(0,2)]
8  
9 #set player to False
10 player = False
11  
12 while player == False:
13 #set player to True
14     player = input("Rock, Paper, Scissors?")
15     if player == computer:
16         print("Tie!")
17     elif player == "Rock":
18         if computer == "Paper":
19             print("You lose!", computer, "covers", player)
20         else:
21             print("You win!", player, "smashes", computer)
22     elif player == "Paper":
23         if computer == "Scissors":
24             print("You lose!", computer, "cut", player)
25         else:
26             print("You win!", player, "covers", computer)
27     elif player == "Scissors":
28         if computer == "Rock":
29             print("You lose...", computer, "smashes", player)
30         else:
31             print("You win!", player, "cut", computer)
32     else:
33         print("That's not a valid play. Check your spelling!")
34     #player was set to True, but we want it to be False so the loop continues
35     player = False
36     computer = t[randint(0,2)]
Experienced programmers will be very quick to point out that there are better ways to write a “Rock, Paper, Scissors” Python game. But, for
a beginner, it’s very important to be able to understand and follow the program. Let’s break it down…

First, we import randint from the random module. This is how our computer opponent will play.

Then we create a list of play options:

Python
1 #create a list of play options
2 t = ["Rock", "Paper", "Scissors"]

There are three possible plays you and the computer can make on each turn, “Rock”, “Paper” and “Scissors”.

Next we setup our players, the computer and you:

Python
1 #assign a random play to the computer
2 computer = t[randint(0,2)]
3  
4 #set player to False
5 player = False

We assign a random play to the computer using our list, t, and the randint function. Why (0,2)? Remember that computers start counting at
0. So “Rock” is in the 0 position, “Paper” is in the 1, and so on. Unlike playing RPS with friends in meatspace, the computer has made its play
and is waiting for you to take your turn. Also unlike playing RPS with friends in meatspace, the computer isn’t go to cheat and change its play
after you make yours. We set you, the player, to False. Why? I’m glad you asked. Let’s take a look at the body of our program the while loop:

Python
1 while player == False:
2 #set player to True
3     player = input("Rock, Paper, Scissors?")
4     if player == computer:
5         print("Tie!")
6     elif player == "Rock":
7         if computer == "Paper":
8             print("You lose!", computer, "covers", player)
9         else:

10             print("You win!", player, "smashes", computer)


10             print("You win!", player, "smashes", computer)
11     elif player == "Paper":
12         if computer == "Scissors":
13             print("You lose!", computer, "cut", player)
14         else:
15             print("You win!", player, "covers", computer)
16     elif player == "Scissors":
17         if computer == "Rock":
18             print("You lose...", computer, "smashes", player)
19         else:
20             print("You win!", player, "cut", computer)
21     else:
22         print("That's not a valid play. Check your spelling!")
23     #player was set to True, but we want it to be False so the loop continues
24     player = False
25     computer = t[randint(0,2)]

Once the while loop starts, the computer will patiently wait for you to make a play. As soon as you take your turn, your status changes from
False to True because any value assigned to the variable player makes player True. We use the input() function to pass the new value to the
variable player. Your input will determine which statement is triggered below.

Using nested if/elif/else statements, we check every possible outcome of the game and return a message stating the winner, a tie, or an error.

We use else at the end to catch anything that isn’t “Rock”, “Paper” or “Scissors”. Finally we reset the player value to False to restart the while
loop.

Your first Python game. Easy, right? Next up, for loops (https://thehelloworldprogram.com/python/whats-a-for-loop-python/).

What is The Hello World Program?

You might also like