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
一切正常,问题是它在test2给了我"'O', function vencedor_diagonal at 0x0000000002C29A48"
。
什么错了?!帮帮我搞清楚!
答案 0 :(得分:3)
您想要返回函数的结果,而不是函数本身。或者更确切地说,由于您的所有函数都返回True
或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))