🎮 Build a Rock, Paper, Scissors Game in Python
Welcome back to DarchumsTech! In today’s tutorial, we’re going to build a classic game — Rock, Paper, Scissors. You’ll learn how to work with conditionals, loops, and random choices in Python.
🧠 What You’ll Learn
- Using the
random
module - Working with conditionals and loops
- Creating interactive input-based games
🚀 Step 1: Import and Setup
import random
choices = ["rock", "paper", "scissors"]
We import the random
module to let the computer randomly choose its move.
🕹️ Step 2: Build the Game Logic
while True:
user_choice = input("Enter rock, paper, or scissors (or 'quit' to stop): ").lower()
if user_choice == "quit":
print("Thanks for playing!")
break
if user_choice not in choices:
print("Invalid input. Try again.")
continue
computer_choice = random.choice(choices)
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!")
🎯 How It Works
- The user inputs their move.
- The computer randomly picks a move.
- The program compares both and declares a winner.
🎮 Sample Gameplay
Enter rock, paper, or scissors (or 'quit' to stop): rock
Computer chose: scissors
You win!
Enter rock, paper, or scissors (or 'quit' to stop): scissors
Computer chose: rock
You lose!
Enter rock, paper, or scissors (or 'quit' to stop): quit
Thanks for playing!
🔁 Bonus: Score Counter
Let’s keep track of your score!
user_score = 0
computer_score = 0
while True:
user_choice = input("Enter rock, paper, or scissors (or 'quit'): ").lower()
if user_choice == "quit":
print(f"Final Score — You: {user_score}, Computer: {computer_score}")
break
if user_choice not in choices:
print("Invalid input.")
continue
computer_choice = random.choice(choices)
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!")
user_score += 1
else:
print("You lose!")
computer_score += 1
🏁 Wrap Up
- This game teaches logic, conditionals, and loops in a fun way.
- You can expand it with GUI, multiplayer, or sound effects.
Next Up: We’re building a Dice Roller Simulator using Python and randomization. Stay tuned!
Enjoyed the tutorial? Subscribe to DarchumsTech and explore more hands-on Python projects!
Comments
Post a Comment