[英]How can I check if a function returns a result in an "IF Else" statement
def checkwin():
if checkcolumn(board) or checkdiagonal(board) or checkrow(board):
print(f"The winner is {winner}")
def checktie(board):
# If no blank spaces("-") check it is a tie
global gamerunning
if "-" not in board and :
printboard(board)
print("It is a tie!")
gamerunning = False
I'm trying to add a statement after and
in the function checktie()
to see if the statement "The winner is {winner}"
is posted then we should ignore the checktie()
function.我试图在函数checktie()
之后and
函数中添加一条语句,以查看是否发布了语句"The winner is {winner}"
,然后我们应该忽略checktie()
函数。
I tried inserting a boolean value to checkwin()
but can't call it in the checktie()
function我尝试向checkwin()
插入一个布尔值,但无法在checktie()
函数中调用它
The function needs to actually return a result:该函数需要实际返回一个结果:
def checkwin():
return checkcolumn(board) or checkdiagonal(board) or checkrow(board)
Then you could do:然后你可以这样做:
if "-" not in board and not checkwin():
However, you may want to rework your script because I don't know why checktie()
would call checkwin()
when whatever code is calling checktie()
could simply call checkwin()
itself.但是,您可能想重新编写脚本,因为我不知道为什么checktie()
会调用checkwin()
的任何代码都可以简单地调用checktie()
checkwin()
本身。 For example:例如:
if checkwin():
print(f"The winner is {winner}")
gamerunning = False
elif checktie(my_board): # This would need to return a boolean too.
print("It is a tie!")
gamerunning = False
else:
# Continue game
You can use another variable to determine the result.您可以使用另一个变量来确定结果。
has_won = False
def checkwin():
global has_won
if checkcolumn(board) or checkdiagonal(board) or checkrow(board):
print(f"The winner is {winner}")
has_won = True
def checktie(board):
# If no blank spaces("-") and no winner is declared, check it is a tie
global gamerunning
if "-" not in board and not has_won:
printboard(board)
print("It is a tie!")
gamerunning = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.