Python 某些参数不能在函数中指定吗?

Python 某些参数不能在函数中指定吗?,python,python-3.x,Python,Python 3.x,在我的player\u player函数中,我想调用check\u win函数,这样我就可以在这个连接四的游戏中宣布一个获胜者。我觉得我设置代码的方式只有在输入某个参数时才有效。我想知道在我的代码中是否可以只输入变量标记的值。 抱歉,如果这是一个多余的问题,我是python的初学者 def check_win(token, i, j): if i-3 in range(0, 6) and j+3 in range(0,5): if board[i][j] == token

在我的player\u player函数中,我想调用check\u win函数,这样我就可以在这个连接四的游戏中宣布一个获胜者。我觉得我设置代码的方式只有在输入某个参数时才有效。我想知道在我的代码中是否可以只输入变量标记的值。 抱歉,如果这是一个多余的问题,我是python的初学者

def check_win(token, i, j):
    if i-3 in range(0, 6) and j+3 in range(0,5):
        if board[i][j] == token and board[i-1][j+1] == token and board[i-2][j+2] == token and board[i-3][j+3] == token:
            return True
    if i-3 in range(0, 6) and j-3 in range(0,5):
        if board[i][j] == token and board[i-1][j-1] == token and board[i-2][j-2] == token and board[i-3][j-3] == token:
            return True
    if j+3 in range(0, 5):
        if board[i][j] == token and board[i][j+1] == token and board[i][j+2] == token and board[i][j+3] == token:
            return True
    if i-3 in range(0, 6):
        if board[i][j] == token and board[i-1][j] == token and board[i-2][j] == token and board[i-3][j] == token:
            return True
    else:
        return False

用三个参数定义函数check_win,但用一个参数调用它。正如已经在一篇评论中提到的,我猜您要寻找的是带有I和j的for循环,而不是作为参数。也许是这样的:

    def check_win(token):
        for i in range(0, 6):
            for j in range(0, 6):
                if i-3 in range(0, 6) and j+3 in range(0,6):
                    if board[i][j] == token and board[i-1][j+1] == token and board[i-2][j+2] == token and board[i-3][j+3] == token:
                        return True
                if i-3 in range(0, 6) and j-3 in range(0,6):
                    if board[i][j] == token and board[i-1][j-1] == token and board[i-2][j-2] == token and board[i-3][j-3] == token:
                        return True
                if j+3 in range(0, 6):
                    if board[i][j] == token and board[i][j+1] == token and board[i][j+2] == token and board[i][j+3] == token:
                        return True
                if i-3 in range(0, 6):
                    if board[i][j] == token and board[i-1][j] == token and board[i-2][j] == token and board[i-3][j] == token:
                        return True
        else:
            return False
以下是我的check_win版本:

样板:

board = [['.', '.', '☼', '.', '.', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['○', '○', '○', '○', '☼', '.', '.'],
         ['○', '☼', '○', '☼', '○', '○', '☼'],
         ['○', '☼', '○', '☼', '○', '☼', '☼']]

嗨,Emily-这里包含的代码数量不足以确定问题的答案。特别是,目前还不清楚令牌值到底应该是什么。也许你可以包含更多的代码和/或更清楚地解释每段代码试图做什么以及每个变量的含义?再看一点,也许你想做的是使用循环在电路板中i和j的所有可能值范围内循环?我想你想让电路板成为一个numpy数组,我想你可能想找到一种区分红色和黑色标记的方法。我在问题中添加了更多代码。至于你的第二个评论,你是否建议我把所有的if语句放在一个for循环中,这样我就不必指定I和j参数了@AmberI认为我通过创建一个player1_令牌变量和一个player2_令牌变量来区分我的令牌。你能详细说明一下吗@mikeyi刚刚使用了你的建议,但它这样做是为了让一个完整的棋盘被认为是一名球员的胜利。即使它不起作用,它仍然帮助我更多地理解这一点,谢谢,我没有办法检查它的正确性。你能确认它是否按预期工作吗。。。或者提供一个样本板进行测试。不,没有。我不知道如何做一个测试板,对不起,什么情况下失败了?对角线?好的。我做了一个测试板。我可以说我已经尝试了所有的可能性。对我来说似乎很好。现在我担心的是我是否正确地把握了比赛的目标。你应该形成一条由四个标记组成的水平线、垂直线或对角线。是吗?是的,没错。我一定是输入错了,因为我再试了一次,结果还是不行
board = [['.', '.', '☼', '.', '.', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['.', '○', '☼', '○', '○', '.', '.'],
         ['○', '○', '○', '○', '☼', '.', '.'],
         ['○', '☼', '○', '☼', '○', '○', '☼'],
         ['○', '☼', '○', '☼', '○', '☼', '☼']]