Here’s a simple Python program to play Rock, Paper, Scissors:
pythonCopy codeimport random
def play_game():
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
user_choice = input("Enter rock, paper, or scissors: ").lower()
if user_choice not in choices:
print("Invalid choice! Please choose rock, paper, or scissors.")
return
print(f"Computer chose: {computer_choice}")
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'paper' and computer_choice == 'rock') or \
(user_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
else:
print("You lose!")
if __name__ == "__main__":
play_game()
How It Works:
- Import the random module: This is used to allow the computer to randomly select its choice.
- Define the
play_game
function: This function handles the game logic. - User Input: The user is prompted to enter their choice.
- Computer Choice: The computer randomly picks rock, paper, or scissors.
- Determine the Outcome: The program compares the user’s choice with the computer’s choice and prints out the result.
- Main Check: The program runs the
play_game
function when executed.
This code is a great starting point to practice Python basics like user input, conditionals, and functions.
Leave a Comment