
๐ Day 107/150 – Rock Paper Scissors Game in Python
Rock Paper Scissors is a simple and fun Python game where the player competes against the computer. It is a great beginner project for practicing random selection, user input, conditions, and comparison operators.
In this post, we'll explore three short ways to create a Rock Paper Scissors game in Python.
Method 1 – Basic Game ๐ฎ
The simplest version randomly selects a choice for the computer.
import random
p = input("Choose: ")
c = random.choice(["rock", "paper", "scissors"])
print("You:", p, "Computer:", c)
Sample Output
Choose: rock
You: rock Computer: scissors
Explanation
random.choice() randomly selects one option from the list.
The user's choice is stored in p, while the computer's choice is stored in c.
This is the basic foundation of the game.
Method 2 – Win or Lose ๐
We can add simple conditions to determine whether the player wins.
import random
p = input("Choose: ")
c = random.choice(["rock", "paper", "scissors"])
print("Win!" if (p=="rock" and c=="scissors") or
(p=="paper" and c=="rock") or
(p=="scissors" and c=="paper") else "Lose!")
Sample Output
Choose: paper
Win!
Explanation
The conditions check the three possible winning combinations:
Rock beats Scissors
Paper beats Rock
Scissors beats Paper
If one of these conditions is true, "Win!" is displayed. Otherwise, "Lose!" is displayed.
Method 3 – Win, Lose or Tie ๐ค
We can also handle the situation when both players choose the same option.
import random
p = input("Choose: ")
c = random.choice(["rock", "paper", "scissors"])
print("Tie!" if p==c else "Win!" if
(p=="rock" and c=="scissors") or
(p=="paper" and c=="rock") or
(p=="scissors" and c=="paper") else "Lose!")
Sample Output
Choose: rock
Tie!
Explanation
First, the program checks whether both choices are the same.
If p == c, the result is "Tie!".
Otherwise, it checks the winning combinations. If none match, the player loses.
๐ Comparison of Methods
Method Best For
Basic Game Learning random choices
Win or Lose Practicing conditions
Win, Lose or Tie Building complete game logic
๐ฅ Key Takeaways
random.choice() is useful for randomly selecting the computer's move.
input() takes the player's choice.
if conditions can determine the winner.
and and or help combine multiple game rules.
Comparing both choices allows us to detect a tie.
Rock Paper Scissors is a simple project for practicing Python logic.
๐ฎ Small games like this are a great way to turn Python fundamentals into interactive projects!
๐ Stay tuned for Day 108 of the #150DaysOfPython series!

0 Comments:
Post a Comment