Python 3 Tuple of enum Equality always fails

Viewed 39

I am building a small simulation of Rock, Paper and Scissors, I have a payoff function that returns a tuple of numbers given two actions


def payoff(act1, act2):  
    if act1, act2 == Action.ROCK, Action.PAPER:
        return -1, 1
    elif act1, act2 == Action.PAPER, Action.ROCK:
        return 1, -1
    elif act1, act2 == Action.PAPER, Action.SCISSORS:
        return -1, 1
    elif act1, act2 == Action.SCISSORS, Action.PAPER:
        return 1, -1
    elif act1, act2 == Action.SCISSORS, Action.ROCK:
        return -1, 1
    elif act1, act2 == Action.ROCK, Action.SCISSORS:
        return 1, -1
    else :
        return 0, 0

I sample two actions at random when given as parameters the equality always fails.

action1 = random.sample([Action.ROCK,Action.PAPER,Action.SCISSORS],1)
action2 = random.sample([Action.ROCK,Action.PAPER,Action.SCISSORS],1)

# always returns (0,0)
payoff(action1,action2)

I am not sure if am doing the equality check properly, as you may notice I construct tuples on the fly using (act1,act2)

1 Answers

Try this:

action1 = random.sample([Action.ROCK,Action.PAPER,Action.SCISSORS],1)[0]
action2 = random.sample([Action.ROCK,Action.PAPER,Action.SCISSORS],1)[0]

# always returns (0,0)
payoff(action1,action2)

random.sample() return an python list

Related