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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more