mardi 30 octobre 2018

Return a negative value

I'm trying to implement a function where I have 2 players and their payoffs depends on their actions.

def game(action1,action2):

  if action1 == "a" and action2 == "a":
     payoff1 = 1
     payoff2 = 1
  elif action1 == "a" and action2 == "b":
     payoff1 = -5
     payoff2 = 3
  elif action1 == "b" and action2 == "a":
     payoff1 = 3
     payoff2 = -5
  elif action1 == "b" and action2 == "b":
     payoff1 = 2
     payyoff2 = 2
 return payoff1 , payoff2

Then I would have strategy for this game (example):

def TitForTat(round_num, previous_action):
    if round_num == 0:
       action = "a"
    else:
       action = previous_action
  return action

def AlwaysDefect():
  return "d"

action1 = TitForTat (0,'c')
action2 = AlwaysDefect()

game (action1,action2)

This returns an error:

local variable 'payoff1' referenced before assignment

I tried to initialize them to "0" , but the same. The exact functions works very well if I have all positive values.

Aucun commentaire:

Enregistrer un commentaire