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!

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay