-3
def vencedor_linha(t):
    if (t[0]=='X' and t[1] and t[2]=='X') or (t[3]=='X' and t[4]=='X'  and t[5]=='X') or (t[6]=='X' and t[7]=='X' and t[8]=='X'):
        print("'X'")
        return True
    elif (t[0]=='O' and t[1] and t[2]=='O') or (t[3]=='O' and t[4]=='O'  and t[5]=='O') or (t[6]=='O' and t[7]=='O' and t[8]=='O'):
        print("'O'")
        return True
    else:
        return False

def vencedor_coluna(t):
    if (t[0]=='X' and t[3] and t[6]=='X') or (t[1]=='X' and t[4]=='X'  and t[7]=='X') or (t[2]=='X' and t[5]=='X' and t[8]=='X'):
        print("'X'")
        return True
    elif (t[0]=='O' and t[3] and t[6]=='O') or (t[1]=='X' and t[4]=='O'  and t[7]=='O') or (t[2]=='O' and t[5]=='O' and t[8]=='X'):
        print("'O'")
        return True
    else:
        return False

def vencedor_diagonal(t):   
   if (t[0]=='X' and t[4] and t[8]=='X') or (t[6]=='X' and t[4]=='X'  and t[2]=='X'):
        print("'X'")
        return True
   elif (t[0]=='O' and t[4] and t[8]=='O') or (t[6]=='X' and t[4]=='O'  and t[2]=='O'):
        print("'O'")
        return True
   else:
        return False

def determina_vencedor(t):
    if vencedor_coluna(t):
        return vencedor_coluna
    elif vencedor_linha(t):
        return vencedor_linha
    elif vencedor_diagonal(t):
        return vencedor_diagonal
    else:
        return False

所以,如果我们使用:

test1=('O',' ',' ',' ','O',' ',' ',' ','O')

test2=(' ',' ',' ','X',' ',' ',' ',' ',' ')

它应该返回我,determina_vencedor(test1)->'O'determina_vencedor(test2)->False

一切都很好test2,问题是它"'O', function vencedor_diagonal at 0x0000000002C29A48"在 test2 给了我。

怎么了?!帮我弄清楚!

4

1 回答 1

3

您想返回函数的结果,而不是函数本身。或者更确切地说,由于您的所有函数都返回Trueor False,因此您希望True在函数返回时返回True

def determina_vencedor(t):
    if vencedor_coluna(t):
        return True

    if vencedor_linha(t)
        return True

    if vencedor_diagonal(t)
        return True

    return False

这可以组合成一个循环:

def determina_vencedor(t):
    for func in (vencedor_coluna, vencedor_linha, vencedor_diagonal):
        if func(t):
            return True
    return False

或者您可以any()用来测试所有功能:

def determina_vencedor(t):
    return any(f(t) for f in (vencedor_coluna, vencedor_linha, vencedor_diagonal))
于 2013-03-07T17:28:54.827 回答