if 语句末尾的语法错误

Syntax error at end of if statement

当我 运行 这段代码时,我在下一行的 : 符号处遇到语法错误 ==> 如果 (total_size != ((len(fleet_grid)*len(fleet_grid[0])-total_size)):

这是为什么?

def validate_character_count(fleet_grid, ship_characters, ship_sizes):
"""(list of list of str, list of str, list of int) -> bool
Checks that the fleet on the grid matches the character and size description

"""

    validity = True
    total_size = 0

     for size in ship_sizes:
        total_size += int(size)
     for row in fleet_grid:
        for position in row:
            if not (position.isnumeric() and position.isalpha()):
                total_size += 1
                if (total_size != ((len(fleet_grid)*len(fleet_grid[0])-total_size)):
                    validity = False

可能缺少结尾 ')' ? 你有 5 个 '(' 但有 4 个 ')'

那是因为语句中多了一对左大括号

if (total_size != ((len(fleet_grid)*len(fleet_grid[0])-total_size)):

在这里,你有两个 (( 在 len 之前。

缺少结束符“)”。我建议您在 运行 程序时阅读输出。最好在 ipython 如果你这样写:if(some_variable:

输出将是 In [8]: File "your_file.py", line 13 if(some_variable: ^ SyntaxError: invalid syntax