In this Python tutorial, we will create a rock, paper, scissors game with a virtual player. The game will cover Python topics such as class objects, defining functions, if statements, and loops. At the end of each game session, the user will be able to continue to play the game or play continuously.
Import Python Modules
The Python modules sys, random and time will need to be imported to create a few of the Python functions in the rock, paper, and scissors game.
1 2 3 | from sys import exit import random import time |
Create Class Methods and Objects
The RockPaperScissors() class will call the variable play_true as True until the user declares No in not replaying the game in the replayGame() function. Once the user inputs No, the play_true variable will declare False and the game is over. In the playGame() function there are multiple loops and if statements. The first while loop declares the game as True and to keep playing until this variable is False. The virtual_guess variable offers three virtual guesses; Rock, Paper and Scissors. In order to randomly pick a choice, we created a for loop that will randomly pick from the virtual_guess variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | def playGame(): play_true = RockPaperScissors.play_true print("\nStarting rock, paper, scissors game! ") time.sleep(2) while(play_true): virtual_guess = ["Rock", "Paper", "Scissors"] for guess in random.sample(virtual_guess, 1): virtual_player_guess = guess virtual_player_guess = virtual_player_guess.upper() while play_true: try: player_guess = input("What will your guess be? Choose rock, paper or scissors? ") player_guess = player_guess.upper() if player_guess != "ROCK" and player_guess != "PAPER" and player_guess != "SCISSORS": print("Enter rock, paper or scissors to play! ") else: play_true = False except: print("Enter rock, paper or scissors to play! ") time.sleep(2) |
Create if..elif… Statements
Based on the output from the player and virtual player, an if statement is created to print who won with the choices from each player. Below is the requirements that are needed to created the if statement:
- If bother players have the same choice, it’s a tie.
- Rock beats Scissors
- Paper beats Rock
- Scissors beats Paper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if player_guess == virtual_player_guess: print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: Game resulted in a tie!") elif player_guess == "ROCK" and virtual_player_guess == "SCISSORS": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You won!") elif player_guess == "ROCK" and virtual_player_guess == "PAPER": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You lost! ") elif player_guess == "PAPER" and virtual_player_guess == "SCISSORS": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You lost!") elif player_guess == "PAPER" and virtual_player_guess == "ROCK": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You won!") elif player_guess == "SCISSORS" and virtual_player_guess == "PAPER": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You won!") elif player_guess == "SCISSORS" and virtual_player_guess == "ROCK": print(f"\nVirtual player guessed {virtual_player_guess} and you guessed {player_guess}: You won!") replayGame() |
Play Again or Exit
The replayGame() function prompt the user to enter Yes to play again or No to exit the game. Once the user inputs Yes, the game will start over and restarting the game over. If the user picks No, it will set the variable play_true to False and the game will exit. As stated earlier, the play_true is set to true in class RockPaperScissors() and called with the while loop as True to keep playing until this variable is set to False.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def replayGame(): play_true = RockPaperScissors.play_true while(play_true): try: replay = input("\nWould you like to play again, Yes or No? ") replay = replay.upper() print(replay) if replay == "YES": print("Let's play again!") playGame() elif replay == "NO": print("Until next time...") play_true = False except: print("Not valid! ") playGame() |
I hope this Python rock, paper, scissors game was fun and helpful to build knowledge in Python class methods, class objects, for loops, while loops and random variable generation.