返回负值

时间:2018-10-30 17:31:42

标签: python list if-statement negative-number

我正在尝试实现一个有2个玩家的游戏,他们的收益取决于他们的行动。

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
     payoff2 = 2
 return payoff1 , payoff2

然后我将为该游戏制定策略(示例):

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

def AlwaysDefect():
  return "b"

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

game (action1,action2)

这将返回错误:

local variable 'payoff1' referenced before assignment

我试图将它们初始化为“ 0”,但是还是一样。 如果我所有的值都是正数,则确切的函数会很好地工作。

编辑:

很抱歉输入错误。并且函数AlwaysDefect()返回“ b”和“ d”。

5 个答案:

答案 0 :(得分:2)

您的if语句arent捕捉到的动作是c还是d,这会导致payoff1和payoff2未被初始化。

答案 1 :(得分:1)

函数调用中的太多内容。使用字典:

def game(action1, action2):
    payoffs = {
        ('a', 'a'): (1, 1),  ('a', 'b'): (-5, 3),
        ('b', 'a'): (3, -5), ('b', 'b'): (2, 2)
    }
    return payoffs.get((action1, action2), (0, 0))

答案 2 :(得分:0)

您是说AlwaysDefect函数返回'd'或'b'吗?

现在它返回'd',您将不会进入if语句,也不会为payoff1或payoff2定义

还有一个错字:payyoff2-> payoff2

您可以设置默认语句以对其进行修复(在else部分):

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
     payoff2 = 2
  else:
     payoff1 = 0 # or any other value
     payoff2 = 0 # or any other value
  return payoff1, payoff2

答案 3 :(得分:0)

您应该将两种收益都初始化为某种东西,因为如果您的行为不属于特定情况,您最终将一无所获。因此,添加一个默认值else,以便您的函数返回默认收益,以下代码应有助于您解决问题伙伴。

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
        payoff2 = 2
    else:
        payoff1= 0
        payoff2=0


    return payoff1 , payoff2

从技术上讲,这应该可以防止您出现该参考错误。

答案 4 :(得分:0)

此代码中存在一些问题。

def game(action1,action2):
  # 1. this if/else chain only handles input values of "a" or "b"
  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
     # 2. this is misspelt, so payoff2 never gets declared for input "b","b"
     payyoff2 = 2

 return payoff1 , payoff2
  1. game 的代码可用于("a","b") x ("a","b")中的输入,但没有错误检查,实际上您发布的代码是提供提供超出该范围的输入
  2. 其中一个有效输入("b","b")仍然会失败,因为您错失了“支付”。冗长的代码越少,拼写错误的机会就越少,但这确实是您应该在测试中找到的。

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

在将参数传递给函数之前,为什么不打印?还是在函数内打印参数?还是进入pdb中的函数并查看它们?

您已在此处将它们设置为"a", "d",并且TitForTat 可以产生了"c""c"函数中根本没有处理"d"game