DEV Community

Hamza Hesham
Hamza Hesham

Posted on

6 3

Rock Paper Scissors Python Rock paper scissors game using python

Hello,
Today we will make Rock paper scissors game using python.
First, we will import random and define a new function that Take all the possibilities in the game.

import random
print("welcome") 
chars = "RPS"
def rps(player1,player2):
    if player1 == "R" and player2 == "P":
        return "computer won!!"
    elif player1 == "P" and player2 == "R":
        return "player1 won!!"
    elif player1 == "P" and player2 == "S":
        return "computer won!!"
    elif player1 == "S" and player2 == "P":
        return "player1 won!!"
    elif player1 == "R" and player2 == "S":
        return "player1 won!!"
    elif player1 == "S" and player2 == "P":
        return "computer won!!"
    elif player1 not in chars:
        return "wrong input"
    elif player1 == player2:
        return "tie"
Enter fullscreen mode Exit fullscreen mode

Second, we will make a new variable and assign it to True then we will make a while loop, inside the loop we will assign two new variables ,
the last thing that we call the function and print it .

run = True
while run:
       player1 = input("R for rock ,P for Paper,S for scissors : \n")
    player2 = random.choice(chars)
    print(rps(player1,player2))
Enter fullscreen mode Exit fullscreen mode

the final code:

import random
print("welcome") 
chars = "RPS"
def rps(player1,player2):
    if player1 == "R" and player2 == "P":
        return "computer won!!"
    elif player1 == "P" and player2 == "R":
        return "player1 won!!"
    elif player1 == "P" and player2 == "S":
        return "computer won!!"
    elif player1 == "S" and player2 == "P":
        return "player1 won!!"
    elif player1 == "R" and player2 == "S":
        return "player1 won!!"
    elif player1 == "S" and player2 == "P":
        return "computer won!!"
    elif player1 not in chars:
        return "wrong input"
    elif player1 == player2:
        return "tie"
run = True
while run:
    player1 = input("R for rock ,P for Paper,S for scissors : \n")
    player2 = random.choice(chars)
    print(rps(player1,player2))

Enter fullscreen mode Exit fullscreen mode

me playing the game
Alt Text

note: you can only use uppercase characters

you can support me on buy me a coffee :
Buy Me A Coffee
twitter:https://twitter.com/Oxy_oxide
buy me a coffee : https://www.buymeacoffee.com/Oxyoxide
Don't forget to follow me.
Thanks for reading.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️