Now it's time to build our first game - the classic Rock, Paper, Scissors.
If you haven't read the previous sections yet, start with that, we will use much of what we learned there for this.
How does it work?
We don't start with code - we start with a plan! We can plan using pen and paper, or by writing comments in a python file.
We want the computer to secretly pick rock, paper or scissors completely at random. If we let rock = 1, scissors = 2, paper = 3, we can use the random module to generate a number between 1 and 3 for this.
After that, we want to ask the user what their selection is: 1, 2 or 3. For this, we use input.
Depending on the computer's and the user's selections, the program uses if-statements to decide whether the user won, lost, or if it's a draw.
The final structure should be something like the ofllowing:
Try to replace the comments above with proper code before reading on. Don't worry about it not being perfect.
What we have here is often called a "specification" - a complete description of what is to be done.
The computer's choice
We previously used the random module (in the modules section), and we can do the same thing here. We use randint to generate a number between 1 and 3, and use a variable named "computer" to store what the computer selected - rock, scissors, or paper.
# Generate a random number between 1-3 and decide to computer's choice import random number = random.randint(1, 3) if number == 1: computer = "rock" elif number == 2: computer = "scissors" else: computer = "paper"
Note: This program doesn't provide any result yet, but the computer variable will be used later on.
The user's choice
Here, there is not much more to do than to ask:
# Ask the user for their choice user = input("rock, paper or scissors?")
The user's choice is stored in the variable "user".
If both the computer and the user picked equally, the result is a tie. We express that as:
if computer == user: print("It's a tie")
Otherwise, there are a couple of options, for example that the computer picked "rock" and the user picked "paper", which means that the user wins. We could write that as:
if computer == "rock" and user == "paper": print("You won!")
All that's left now is to fill in the remaining possibilities.
A complete program
Let's put all the parts together. Study the code carefully:
# Generate a random number between 1-3 to decide the computer's choice import random mumber = random.randint(1, 3) if mumber == 1: computer = "rock" elif mumber == 2: computer = "scissors" else: computer = "paper" # Ask the users for their choice user = input("rock, scissors eller paper?") # Compare the choices and print out who won if computer == user: print("Oavgjort") elif computer == "rock" and user == "paper": print("You won!") elif computer == "rock" and user == "scissors": print("The computer won!") elif computer == "scissors" and user == "paper": print("The computer won!") elif user == "rock" and computer == "paper": print("The computer won!") elif user == "rock" and computer == "scissors": print("You won!") elif user == "scissors" and computer == "paper": print("You won!") else: print(“Did you entered something strange?”)
In the video tyou can see how we build thise game, and you can copy and use the complete code above.
As you notice, the game is over after we've played it one time. Wouldn't it be nice if the game restarted when we've played it, especially if it ends in a draw? We will talk about that in the next section.
In this video, we look att another game involving a turtle that moves according to user's answer.
- Specification: A description of what a program, app or game should contain and what users should be able to do with it. We can write it with comments before we start programming.
- If-statement: The keyword "if" followed by a logical condition allows our program to make decisions.
- Module: We can reuse code that others have written, in our programs. To not get overwhelmed by all the code, we divide it up into modules that can be imported as needed. A module contains one or more functions.
- Variable: A part of the computer's memory where we can store something, like a piece of text, a name, or a number.