【问题标题】:Syntax error at end of if statementif 语句末尾的语法错误
【发布时间】:2016-07-02 23:01:46
【问题描述】:

当我运行此代码时,我在以下行中的 : 符号处收到语法错误 ==> if (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

【问题讨论】:

  • 是的,那是因为你的括号不匹配。错误没有告诉你吗?

标签: python syntax syntax-error


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 2019-04-18
        • 1970-01-01
        • 2011-09-22
        • 2015-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多