查找列表中是否有空格(返回布尔值)



正在寻找关于tick-tac-toe练习的帮助:

我有以下信息:

test_board=['#','X','O','X','O','X','O',''X']

def space_check(board,position):
free_space = ""
if free_space in board[position]:
return True
else:
return False

当运行函数时,我看不到False返回,只有True:

space_check(test_board,7)
Output: True
space_check(test_board,9)
Output: True

If free_space in board[position]

您正在搜索一个"在不在字符串列表中的字符串中,所以结果总是真的,因为"quot;总是存在于任何字符串中。

您只需使用相等运算==而不是in

使用以下内容更改您的功能:

def space_check(board,position):
free_space = ""
return free_space == board[position]
test_board = ['#','X','O','X','O','X','O','','O','X']
space_check(test_board, 7)
Output : True
space_check(test_board, 9)
Output: False

最新更新