DEV Community

Krishna Agarwal
Krishna Agarwal

Posted on

Create Rock-Paper-Scissor Game in Python

First Import Library "random"
with:

import random
Enter fullscreen mode Exit fullscreen mode

Set Default Score with:

user_wins= 0
computer_wins= 0
Enter fullscreen mode Exit fullscreen mode

Give Options:

options= ["Rock", "Paper", "Scissor"]

Enter fullscreen mode Exit fullscreen mode

If Users want to quit:

while True:
    user_input= input("Enter Rock, Paper, or Scissors or Q to Quit: ")
    if user_input == "q" or user_input == "Q":
        break

Enter fullscreen mode Exit fullscreen mode

What will Happen when user enters Rock, Paper or Scissor:

    if user_input not in options:
        continue
    random_number= random.randint(0,2)
    # rock: 0, paper: 1, scissor: 2
    computer_pick= options[random_number]
    print("Computer picked: ", computer_pick + "\n" "You picked: ", user_input)

    if user_input == "Rock" and computer_pick == "Scissor":
        print("You win!")
        user_wins += 1

    elif user_input == computer_pick:
        print("It's a tie!")

    elif user_input == "Paper" and computer_pick == "Rock":
        print("You win!")
        user_wins += 1

    elif user_input == "Scissor" and computer_pick == "Paper":
        print("You win!")
        user_wins += 1

    else:
        print("You lose!")
        computer_wins += 1
Enter fullscreen mode Exit fullscreen mode

Print the final score:

print("You won: ", user_wins, "times.")
print("Computer won: ", computer_wins, "times.")
if user_wins > computer_wins:
    print("You are the winner!")
elif user_wins < computer_wins:
    print("Computer is the winner!")
else:
    print("It's a tie!")
print ("Thanks for playing!")
Enter fullscreen mode Exit fullscreen mode

Check out full code here: Source Code

Output:

Image description

Keep Coding & Keep Learning
Stay Tuned for more!

Top comments (0)