DEV Community

Krishna Agarwal
Krishna Agarwal

Posted on

2 2

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!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs