我有这样的方法:
def _play(self):
carda = self._carda
cardb = self._cardb
if carda.is_trump and cardb.is_trump:
winner = self._winbyrank()
return winner
if not carda.is_trump and not cardb.is_trump:
if carda.suite == cardb.suite:
winner = self._winbyrank()
return winner
else:
winner = self._winbyfirst()
return winner
if carda.is_trump and not cardb.is_trump:
winner = self._winbytrump()
return winner
elif not carda.is_trump and cardb.is_trump:
winner = self._winbytrump()
return winner
这偶尔会(而且对我来说出乎意料地)回归None
。当我用以下内容替换它时,它解决了问题。
def _play(self):
carda = self._carda
cardb = self._cardb
if carda.is_trump and cardb.is_trump:
winner = self._winbyrank()
if not carda.is_trump and not cardb.is_trump:
if carda.suite == cardb.suite:
winner = self._winbyrank()
else:
winner = self._winbyfirst()
if carda.is_trump and not cardb.is_trump:
winner = self._winbytrump()
elif not carda.is_trump and cardb.is_trump:
winner = self._winbytrump()
return winner
这不是 Python 解释器中的错误吗?方法不应该return
是winner
值并退出方法而不是仍然移动到最后并返回None
吗?我正在使用 Python 2.7.3