Python Rock Paper Scissors Game
This is a simple rock paper scissors game in Python. Here is how it works:
- It imports the random module to generate a random choice for the computer.
- It defines a list of options — rock, paper, scissors.
- `get_computer_choice()` randomly picks one of these options using `random.choice()`.
- `get_player_choice()` prompts the user to enter their choice, loops until they enter a valid choice, and returns their selection.
- `check_winner()` compares the player and computer choices. If they are the same, it’s a tie. Otherwise, it checks the `beats()` function to see who wins.
- `beats()` has a list of tuples defining which choice beats which other choice. It checks if the player/computer pair matches one of these winning combinations.
- `play_game()` contains the main game loop. It gets the player and computer choices, prints them, checks for a winner, and asks if the player wants to play again.
- The `if __name__ == ‘__main__’` block calls `play_game()` to start the game when the program is run.
So in summary, it’s a simple game loop that uses random selection and predefined rules to play rock paper scissors against the computer. The key Python concepts used are functions, loops, conditionals, user input, and modules.
import random
OPTIONS = ["rock", "paper", "scissors"]
def get_computer_choice():
return random.choice(OPTIONS)
def get_player_choice():
while True:
choice = input("Enter your choice (rock, paper, scissors): ").lower()
if choice in OPTIONS:
return choice
def check_winner(player, computer):
if player == computer:
return "Tie!"
elif beats(player, computer):
return "You won!"
return "Computer won!"
def beats(one, two):
wins = [('rock', 'scissors'),
('paper', 'rock'),
('scissors', 'paper')]
return (one, two) in wins
def play_game():
while True:
player = get_player_choice()
computer = get_computer_choice()
print("Computer played:", computer)
winner = check_winner(player, computer)
print(winner)
play_again = input("Play again? (y/n) ").lower()
if play_again != 'y':
break
if __name__ == '__main__':
play_game()
This is a simple rock paper scissors game in Python. Here is how it works:
- It imports the random module to generate a random choice for the computer.
- OPTIONS is a list of the 3 possible choices — rock, paper, scissors.
- get_computer_choice() randomly picks one of these options using random.choice().
- get_player_choice() prompts the user to enter their choice, validating it is one of the options.
- check_winner() compares the player and computer choices. It checks if they are equal for a tie. Otherwise, it uses the beats() function to see if the player’s choice beats the computer’s.
- beats() has a list of tuples defining which choices beat which other choices. It checks if the player/computer pair matches one of these winning combinations.
- play_game() contains the main game loop. It gets the player and computer choices, prints them, checks the winner, and asks if the player wants to play again.
- The main block calls play_game() to start the game.
So in summary, it’s a basic loop getting input, comparing choices, determining a winner, and repeating until the player decides to quit. The random and validation logic makes it a fully functional rock paper scissors game in Python.