Rock, paper, scissors

Do you want to play rock, paper, scissors against your computer? Well, here’s how to do it…(Skip to the code at the end if you want to play straight away.)

When I looked this up online there were so many ways to write the code for this game, from introducing loops through to using integers instead of words. It’s worth a Google once you’ve read this blog if you’re interested in what others have come up with. I decided I’d stick with a simple while loop, use words as my input and added some ascii art to make it visually more interesting.

The first thing you’ll need to do, and the only thing you’ll need to import is:

import random

Otherwise your computer won’t know how to select a random item from the variables we’re about to create.

Like I mentioned earlier, I’ll place all of this in a while loop. You’ll see from the indentation that everything inside the loop is indented and I’ll show you how to end the loop at the end. The loop allows the player to keep playing until they decide that they are done, without having to re-run the code every time. We’re going to make the assumption that the player wants to keep looping the game until they say otherwise.

We’ll also use some ascii art (give it a Google if you haven’t heard of it already), so that we can use these as images in the game.

play_again = "y"
while play_again == "y": 
   
    rock = '''
    _____
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

    paper = '''
    ______
---'   ____)__
          ______)
          _______)
         _______)
---.__________)
'''

    scissors = '''
    ______
---'   ____)__
          ______)_
       __________)
      (____)
---.__(___)

Next we’re going to add an input, so that the user can make a choice between rock, paper and scissors:

  user_choice = (input("Enter your choice: rock, paper or scissors... ")).lower()

Adding .lower() mans that the computer will always make the input lower case, this will be useful later. If the user enters SCISSors, scissors, Scissors…It’ll always read it as scissors.

    if user_choice == "scissors":
      print("You chose:" + scissors)
    elif user_choice == "rock":
      print("You chose:" + rock)
    elif user_choice == "paper":
      print("You chose:" + paper)
An if statement is used in Python for decision making. We want to make a decision between rock, paper and scissors.
We are using if, elif (else-if) and else in this game. You need to start an if statement with an if, but you can end with just the if, an elif or an else, depending on what you need. 
== means 'equals to'. 
if, else and elifs always end in a colon and are lower case. 
Ifs and elifs need to be explained so that the computer knows what you're looking for e.g.
 if user_choice == "scissors": 
In this instance we're saying if the user types in 'scissors' then move on to the next line, which is 
if user_choice == "scissors": 
    print("You chose:" + scissors)

You’ll notice in the line if user_choice == “scissors’”:  scissors is in quotes, this is because it’s looking for a lower case string inputted by the user.

You’ll also notice that in the print line the word scissors isn’t in quotes. When calling variable names (in this case the scissors ascii art) you don’t need quotes.

We want to print words as string “You chose” and we want to add + (concatenate) those words to the variable we made earlier called scissors.

An ‘else’ is used as a way of saying ‘if something else happens’ at the end of an if statement (if you want one) so they don’t need any explanation, if things don’t happen that are part of the rest of your if statement, then the else will deal with it as you require.

E.g. if someone writes “hammer”, the ‘else’ would catch that it’s not rock, paper or scissors and you could leave them a message such as:

else:

        print("Please type rock, paper or scissors.")
    vars = [rock, paper, scissors]
    computer_choice = random.choice(vars)
    print("The computer chose:" + computer_choice)
or...
or...

Our ascii art variable names are rock, paper and scissors. We can put these together in a list [square brackets] and make them one variable called vars.

This is where we can use random, which we imported earlier.

The computer_choice variable is using random.choice (variable_name) to pick a random item from the variable that we just created, vars.

The print statement will print “The computer chose” and random choice of one of the ascii art variables.

Now we can use an if statement and embedded (the indented) if statements to tell it what we want to do.

In the first if statement we can see that the user chooses “scissors”.

We’ve embedded an if statement within this first if statement to tell the computer what we want to do every time the user chooses “scissors”.

We’re telling the computer that if it also chooses scissors it needs to print “It’s a tie…”, if the computer chooses rock then the users loses and if the computer chooses paper then the user wins.

We then have two more if statements with embedded if statements below the first one to cover the user choosing both rock and paper.

All of the if statements end with one else which will print a message if the user fails to type rock, paper or scissors e.g. if they type “hammer”.

    if user_choice == "scissors":
        if computer_choice == scissors:
            print ("It's a tie! Nice scissors.")
        elif computer_choice == rock:
            print("You lose! Rock blunts scissors.")
        else:
            print("You win! Scissors cut paper.")
    elif user_choice == "rock":
        if computer_choice == rock:
            print ("It's a tie! Rock 'n' roll.")
        elif computer_choice == paper:
            print("You lose! Paper covers rock.")
        else:
            print("You win! Rock blunts scissors.")
    elif user_choice == "paper":
        if computer_choice == paper:
            print ("It's a tie! There's paper everywhere.")
        elif computer_choice == scissors:
            print("You lose! Scissors cut paper.")
        else:
            print("You win! Paper covers rock.")
    else:
        print("Please choose, rock, paper or scissors.")

Now we are going to ask the user if they want to play again. If you remember from the while loop, we’re assuming that “y” is true. If the users types anything other than “y” or “Y” e.g., “N” or “p” the loop will break and the game will end with a message.

play_again = input("Would you like to play again? Y/N ").lower() 

You’ll notice that the final print statement is not embedded as it’s not part of the loop and only needs to be shown when the user breaks the loop and ends the game.

print("\nThanks for playing!")

If you want to play the game or have a go at editing the code yourself then the full code is below. There are a lot of ways to make this game. We could have made the input an integer so that the user wouldn’t have to type the words e.g. rock = 0, paper = 1 and scissors = 2. Another benefit of this is that it’s be easy to add different languages to the game.

I hope you had fun and we’ll be back with another game soon.

By Hannah Johnstone

In [ ]:

import random
play_again = "y"
while play_again == "y": 
    
    rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

    paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

    scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

    user_choice = (input("Enter your choice: rock, paper or scissors... ")).lower()

    if user_choice == "scissors":
      print("You chose:" + scissors)
    elif user_choice == "rock":
      print("You chose:" + rock)
    elif user_choice == "paper":
      print("You chose:" + paper)

    vars = [rock, paper, scissors]
    computer_choice = random.choice(vars)
    print("The computer chose:" + computer_choice)

    if user_choice == "scissors":
        if computer_choice == scissors:
            print ("It's a tie! Nice scissors.")
        elif computer_choice == rock:
            print("You lose! Rock blunts scissors.")
        else:
            print("You win! Scissors cut paper.")
    elif user_choice == "rock":
        if computer_choice == rock:
            print ("It's a tie! Rock 'n' roll.")
        elif computer_choice == paper:
            print("You lose! Paper covers rock.")
        else:
            print("You win! Rock blunts scissors.")
    elif user_choice == "paper":
        if computer_choice == paper:
            print ("It's a tie! There's paper everywhere.")
        elif computer_choice == scissors:
            print("You lose! Scissors cut paper.")
        else:
            print("You win! Paper covers rock.")
    else:
        print("Please choose, rock, paper or scissors.")

    play_again = input("Would you like to play again? Y/N ").lower() 

print("\nThanks for playing!")
Published
Categorized as blog

Leave a comment

Your email address will not be published. Required fields are marked *