Rock, Paper, Scissor: A classic game
Rock, Paper, Scissors: Creating a Python Game
Rock, Paper, Scissors is a classic game that has entertained people for generations. It’s a game of chance that’s quick to play, easy to learn, and often used to settle disputes or just for fun. This blog will walk you through creating a simple Rock, Paper, Scissors game using Python, allowing you to play against the computer. We’ll explain the code provided and offer insights on how you can create and customize your own game.
Code of Rock, Paper, Scissors:
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type rock, paper, scissors or Q to quit:"). lower()
if user_input == "q":
break
if user_input not in options:
continue
random_number = random.randint(0,2)
#rock: 0, paper: 1, scissor:2
computer_choice = options[random_number]
print("Computer chosed", computer_choice + ".")
if user_input =="rock" and computer_choice =="scissors":
print("You win!")
user_wins +=1
elif user_input == "paper" and computer_choice =="rock":
print("You win!")
user_wins += 1
elif user_input =="scissors" and computer_choice == "paper":
print("You win")
user_wins += 1
elif user_input and computer_choice == "both choice same input":
print("its a tie")
else:
print("You lose!")
computer_wins += 1
print("You win", user_wins, "times.")
print("The computer win", computer_wins, "times")
print("Thanks for playing")
The Game in Python
First, let's take a look at the code and understand its components.
Initialization: The code starts by defining two variables,
user_wins
andcomputer_wins
, set to zero. These variables keep track of how many rounds each player has won.Options: The game uses a list,
options
, which contains the possible choices:"rock"
,"paper"
, and"scissors"
. This list will be used to randomly generate the computer's choice and validate the user's choice.Game Loop: The program runs an infinite loop (
while True
) that prompts the user for input each iteration. The user can type"rock"
,"paper"
, or"scissors"
, or type"Q"
to quit the game.User Input Validation: If the user types an invalid choice (something other than
"rock"
,"paper"
, or"scissors"
), the program continues to the next iteration without processing the round.Computer's Choice: The
random.randint
function is used to generate a random index from 0 to 2. This index is then used to select a choice from theoptions
list for the computer.Determine the Winner: The code compares the user’s choice and the computer’s choice and determines the winner based on the game rules:
Rock beats scissors.
Paper beats rock.
Scissors beat paper.
If the user's choice and computer's choice are the same, it's a tie.
Depending on the outcome, the program either increments
user_wins
orcomputer_wins
.
Display Results: After each round, the program displays whether the user won, lost, or tied the round. When the user decides to quit by typing
"Q"
, the program displays the final scores and thanks the user for playing.
Code Improvements
Handle Ties: The code should have a condition to handle ties when the user's and computer's choices are the same.
User Input Validation: Improve input validation by handling upper and lower case inputs and invalid choices more gracefully.
Conclusion
This simple Rock, Paper, Scissors game demonstrates how easily you can create interactive games with Python. The code provided is a solid starting point for creating your own version of the game. You can expand the code to include more features, such as different game modes, more choices, or a graphical user interface.
Rock, Paper, Scissors is a fun way to practice coding and create an engaging game. Try experimenting with the code, adding new features, and customizing it to your preferences. Have fun coding and playing!