make rock paper scissors in python

We can make a basic version of Rock, Paper, Scissors game using Python's random module and conditional statements. Here's the code:

main.py
import random

def play():
    user = input("What's your choice? 'r' for rock, 'p' for paper, 's' for scissors\n")
    computer = random.choice(['r', 'p', 's'])

    if user == computer:
        return 'It\'s a tie!'

    if is_win(user, computer):
        return 'You won!'

    return 'You lost!'

def is_win(player, opponent):
    if (player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r'):
        return True

print(play())
487 chars
20 lines

In this code, the play() function takes user's choice as input and uses the random module to generate a random choice for the computer. Then it checks the user's choice against the computer's choice to determine the winner using the is_win() function.

The is_win() function implements the basic rules of Rock, Paper, Scissors game - rock beats scissors, scissors beats paper and paper beats rock.

To play the game, simply run the play() function.

gistlibby LogSnag