ScriptCrew

Rock, Paper, Scissors Game Tutorial

Natalie

Step-by-Step Instructions

Step 1: Setting up your file

Example Code: Rock-paper-scissors game

Fork this Repl: Rock-paper-scissors Starter Code

This is what you should see:

image

Step 2: Create Your Variables

You will have three variables that you will create. In Python, you typically name your variables with an underscore between words:

new_variable = 0

For this game, you need to create 3 variables: player_wins, computer_wins, and a list of what you or the computer can choose. Set player_wins and computer_wins to 0. To make a list, you do:

new_list = ["option1", "option2", "option3"]

Step 3: Make a while loop

To start a while loop:

while True:
    #code

Remember, in Python, indentation is very important. If you don’t indent code, it will not actually be in the loop.

Step 4: Inside the Loop - User input

We want this game to be the player vs. computer, so we need a way to know what the user wants to pick:

user_input = input("Words").lower()

The .lower changes everything the user types to lowercase letters. Replace "Words" with what you want the player to see when they play (tell them what to do).

Step 5: Inside the Loop - Using user input

The user will be able to type one of 3 things “rock”, “paper”, or “scissors” Add a quit option so it gives the user the chance to end the game and see their wins vs the computer wins. Use break to do this:

if user_input == "q":
    break

To make sure the user types in a valid response you can check whether user input is in the list of options you made. Use continue to try again:

if user_input not in list:
    print("Try again message")
    continue

Step 6: Computer choice

Create a variable for a random number between 0 and 2:

The 0 to 2 represents the indices of the list of options you made. (Remember lists start at an index of 0)

random_number = random.randint(0, 2)

Use that number to get the computers option using the list, then print what the computer picked:

computer_pick = options[random_number]
print("Computer picked", computer_pick)

Step 7: Win or Lose?

For the user to be able to win or lose the game, you will need to compare their choice to the computer choice.

To do this you will use if statements and compare each possible situations. To make this shorter you can only write when the user wins or tie and use only one elif for all user loses

if user_input == "rock" and computer_pick == "scissors":
    print("You win!!")
    user_wins += 1
elif user_input == "paper" and computer_pick == "rock":
    print("You win!!!")
    user_wins += 1
elif user_input == "scissors" and computer_pick == "paper":
    print("You win!")
elif user_input == computer_pick:
    print("Wow, it's a tie!")
else:
    print("You lost")
    computer_wins += 1

Pay attention to indentation, as it determines if code is part of a loop or block.

Step 8: Ending the game

When the user types "q" to quit, end the game and show the results:

print("You won", user_wins, "times")
print("The computer won", computer_wins, "times")
print("Goodbye!")