Beginner Python Tutorial: Rock, Paper, Scissors

RedXIII | April 14, 2021, 4:30 p.m.

Learn programming concepts quickly by making a familiar game.

Python is one of the most popular computer programming languages in the world. It’s considered easier to learn than other popular languages like C++ and Java. This is one reason why Python is quickly becoming a popular choice for beginning computer science students.

In this lesson we’ll cover some of the basics of working with Python by creating a simple game. Everyone knows paper, rock, scissors.

Using Python, we can play against the computer. In the process, we’ll cover some of the fundamental features of running Python code.

Setup

If you don’t already have the Python language installed on your computer, you’ll need to visit python.org and download it. If you’re on a Windows machine, be sure to tell the installation wizard to add Python to your environment variable PATH (it will prompt you to do this during the install).

Mac and Linux users may need to upgrade to Python 3 to follow along, as this lesson uses the latest version of Python 3.

You’ll know that Python is installed by pulling up the command prompt or terminal and typing ‘python.’

You should see a message that includes the version of Python installed on your system.

Once installed, Python can run scripts directly from the terminal. To run a Python file, first navigate to the folder containing the file, using either the terminal or command prompt depending on your OS.

Once the directory containing the file is located, type ‘python’ followed by the name of the file you’d like to execute. (This assumes that Python was added to the environment variables PATH).

Making the Game

For a project like this, we’ll need two main elements: a loop and user input. With Python, we can use a while loop to construct a never-ending game loop that can be used to control the flow of the game.

With each loop, the program will pause to ask for user input, and then the input will be evaluated for validity. There will also need to be a way out of the loop so the player can leave the game.

So this will be our while loop:

new_input = ""while new_input != 'quit':
print("Please choose paper, rock, or scissors. Or type 'quit' to quit the game.")
new_input = input("What is your choice?")
player_choice = "scissors" is_valid = True if new_input == "paper":
player_choice = "paper"
elif new_input == "rock":
player_choice = "rock"
elif new_input == "scissors":
# the choice is set to scissors by default
pass
else:
print("That's not a valid choice.")
is_valid = False
# only do the game logic if the choice was valid
if is_valid:
pass

The user’s input is stored as a string and then validated using a conditional statement. If the input is invalid, the game logic is skipped.

To complete the game, we’ll need some game logic. First, the computer needs to take a turn, making a random choice each time. Then we’ll have to match the computer’s choice against the player’s choice and determine the winner.

For these tasks we’ll need two functions, one to handle the computer’s turn and the other to decide who won the match.

In order for the computer to make a choice, Python will need access to the random library. Add the following code to the top of the program file.

import random

The computer_turn() function will choose a random integer from 0 to 2, inclusively. Then we’ll match each number to a choice, either ‘rock’, ‘paper’, or ‘scissors.’

def computer_turn():
r = random.randint(0,2)
computer_choice = "scissors"
if r == 0:
computer_choice = "rock"
elif r == 1:
computer_choice = "paper"
return computer_choice

Once the computer has made a choice, a winner can be determined by comparing the strings. A second function, determine_winner(), handles this portion of the game logic.

In this exercise, I’ve written out every possible scenario between the computer and the player.


def determine_winner(player_choice, computer_choice):
if computer_choice == player_choice:
print("It's a tie.")
# computer victory scenarios.
elif computer_choice == "rock" and player_choice == "scissors":
print("Rock beats paper. Computer wins.")
elif computer_choice == "paper" and player_choice == "rock":
print("Paper beats rock. You lose.")
elif computer_choice == "scissors" and player_choice == "paper":
print("Scissors cuts paper. Computer wins.")
# player victory scenarios
elif computer_choice == "rock" and player_choice == "paper":
print("Paper beats rock. You win!")
elif computer_choice == "paper" and player_choice == "scissors":
print("Scissors beats paper. You win.")
elif computer_choice == "scissors" and player_choice == "rock":
print("Rock crushes scissors. You win.")

Lastly, we’ll need to make calls to both the computer_turn() and the determine_winner() functions from within our while loop. With these calls, the game is complete.

The game is played in the terminal.

Conclusion

And that’s it. The rock, paper, scissors program is only about sixty lines of code. I’ve included the completed program below, but I highly encourage you to type it out yourself if you’re new to programming with Python.

Thanks for following along. I hope you enjoyed this tutorial and learned something about Python and computer programming. Comment below if you have any feedback or questions about the exercise.

import randomdef computer_turn():
r = random.randint(0,2)
computer_choice = "scissors"
if r == 0:
computer_choice = "rock"
elif r == 1:
computer_choice = "paper"
return computer_choicedef determine_winner(player_choice, computer_choice):
if computer_choice == player_choice:
print("It's a tie.")
# computer victory scenarios.
elif computer_choice == "rock" and player_choice == "scissors":
print("Rock beats paper. Computer wins.")
elif computer_choice == "paper" and player_choice == "rock":
print("Paper beats rock. You lose.")
elif computer_choice == "scissors" and player_choice == "paper":
print("Scissors cuts paper. Computer wins.")
# player victory scenarios
elif computer_choice == "rock" and player_choice == "paper":
print("Paper beats rock. You win!")
elif computer_choice == "paper" and player_choice == "scissors":
print("Scissors beats paper. You win.")
elif computer_choice == "scissors" and player_choice == "rock":
print("Rock crushes scissors. You win.")
new_input = ""while new_input != 'quit':
print("Please choose paper, rock, or scissors. Or type 'quit' to quit the game.")
new_input = input("What is your choice?")
player_choice = "scissors" is_valid = True if new_input == "paper":
player_choice = "paper"
elif new_input == "rock":
player_choice = "rock"
elif new_input == "scissors":
# the choice is set to scissors by default
pass
else:
print("That's not a valid choice.")
is_valid = False
# only do the game logic if the choice was valid
if is_valid:
print("You chose {}.".format(player_choice))
computer_choice = computer_turn()
print("Computer chose {}.".format(computer_choice)) determine_winner(player_choice, computer_choice)
print("\n")

About Us

Learning at the speed of light.

We created Start Prism to help students learn programming. You can find exercises and recent tutorials below.

Topics Quizzes Tutorials

0 comments

Leave a comment