Rock Paper Scissors with Python

Here's a couple of versions. The first is the most verbose, most explicit version. It plays best two of three.


In [4]:
import random

choices = ["Rock", "Paper", "Scissors"]

def choice():
    selection = random.choice(choices)
    return selection

def winner(player1, player2):
    if player1 == "Rock" and player2 == "Rock":
        result = "Tie"
    elif player1 == "Rock" and player2 == "Paper":
        result = "Player 2 wins"
    elif player1 == "Rock" and player2 == "Scissors":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Paper":
        result = "Tie"
    elif player1 == "Paper" and player2 == "Rock":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Scissors":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Scissors":
        result = "Tie"
    elif player1 == "Scissors" and player2 == "Rock":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Paper":
        result = "Player 1 wins"
    return result

player_1_wins = 0
player_2_wins = 0

while player_1_wins != 2 and player_2_wins != 2:
    player_1 = choice()
    player_2 = choice()
    winr = winner(player_1, player_2)
    print("Player 1 plays %s" % player_1)
    print("Player 2 plays %s" % player_2) 
    print(winr)
    # Here's where we'll overwrite the counts of the wins to determine best two of three
    if winr == "Player 1 wins":
        player_1_wins = player_1_wins + 1
        print("Player 1 total wins: %i" % player_1_wins)
    elif winr == "Player 2 wins":
        player_2_wins = player_2_wins + 1 
        print("Player 2 total wins: %i" % player_2_wins)
    else:
        pass


Player 1 plays Scissors
Player 2 plays Paper
Player 1 wins
Player 1 total wins: 1
Player 1 plays Paper
Player 2 plays Rock
Player 1 wins
Player 1 total wins: 2

This version removes the explicit ties and just says if player1 == player2 it's a tie. Less code, same result.


In [2]:
import random

choices = ["Rock", "Paper", "Scissors"]

def choice():
    selection = random.choice(choices)
    return selection

def winner(player1, player2):
    if player1 == player2:
        result = "Tie"
    elif player1 == "Rock" and player2 == "Paper":
        result = "Player 2 wins"
    elif player1 == "Rock" and player2 == "Scissors":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Rock":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Scissors":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Rock":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Paper":
        result = "Player 1 wins"
    return result

player_1_wins = 0
player_2_wins = 0

while player_1_wins != 2 and player_2_wins != 2:
    player_1 = choice()
    player_2 = choice()
    winr = winner(player_1, player_2)
    print("Player 1 plays %s" % player_1)
    print("Player 2 plays %s" % player_2) 
    print(winr)
    # Here's where we'll overwrite the counts of the wins to determine best two of three
    if winr == "Player 1 wins":
        player_1_wins = player_1_wins + 1
        print("Player 1 total wins: %i" % player_1_wins)
    elif winr == "Player 2 wins":
        player_2_wins = player_2_wins + 1 
        print("Player 2 total wins: %i" % player_2_wins)
    else:
        pass


Player 1 plays Paper
Player 2 plays Rock
Player 1 wins
Player 1 total wins: 1
Player 1 plays Rock
Player 2 plays Rock
Tie
Player 1 plays Scissors
Player 2 plays Paper
Player 1 wins
Player 1 total wins: 2

This version takes the explicit version and plays a million games of Rock Paper Scissors. The result is ... pretty equal.


In [3]:
import random

choices = ["Rock", "Paper", "Scissors"]

def choice():
    selection = random.choice(choices)
    return selection

def winner(player1, player2):
    if player1 == "Rock" and player2 == "Rock":
        result = "Tie"
    elif player1 == "Rock" and player2 == "Paper":
        result = "Player 2 wins"
    elif player1 == "Rock" and player2 == "Scissors":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Paper":
        result = "Tie"
    elif player1 == "Paper" and player2 == "Rock":
        result = "Player 1 wins"
    elif player1 == "Paper" and player2 == "Scissors":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Scissors":
        result = "Tie"
    elif player1 == "Scissors" and player2 == "Rock":
        result = "Player 2 wins"
    elif player1 == "Scissors" and player2 == "Paper":
        result = "Player 1 wins"
    return result

player_1_wins = 0
player_2_wins = 0
ties = 0

for i in range(1000000):
    player_1 = choice()
    player_2 = "Scissors"
    winr = winner(player_1, player_2)
    #print("Player 1 plays %s" % player_1)
    #print("Player 2 plays %s" % player_2) 
    #print(winr)
    # Here's where we'll overwrite the counts of the wins to determine best two of three
    if winr == "Player 1 wins":
        player_1_wins = player_1_wins + 1
        #print("Player 1 total wins: %i" % player_1_wins)
    elif winr == "Player 2 wins":
        player_2_wins = player_2_wins + 1 
        #print("Player 2 total wins: %i" % player_2_wins)
    else:
        ties = ties + 1

print("Player 1 won %s games. Player 2 won %s. There were %s ties." % (player_1_wins, player_2_wins, ties))


Player 1 won 333277 games. Player 2 won 333557. There were 333166 ties.