【问题标题】:invalid syntax - python无效的语法 - python
【发布时间】:2011-12-06 16:02:02
【问题描述】:

我有一组参数,它们的所有目标都是“search_and”、“search_not”、“search_start”、“search_then”和“filename”

我必须捕获错误的代码是

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None):
        parser.error(usage)
        sys.exit()

这似乎是一个愚蠢的错误,有人能告诉我发生了什么吗?

编辑#1: 我在末尾添加了 ')' 以确保它正确关闭,但它仍然显示无效语法。

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

编辑#2: 这是我目前所拥有的。

    import optparse from OptionParser
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-b", "--all", type="string", dest="search_not", help="search and find the lines that contain words1 not word2")
    parser.add_option("-c", "--all", type="string", dest="search_start", help="search and find a line that starts with word1 or word2")
    parser.add_option("-d", "--all", type="string", dest="search_then", help="search and find a line that has word1 followed by word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="file name"

    if ( options.filename is None) and ( (options.search_and is None) or  (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):

代码运行后: python script.py -a hi bye

我的 if 语句语法无效。

【问题讨论】:

    标签: python syntax if-statement


    【解决方案1】:

    您在: 之前错过了)。你的编辑应该发现这些错误。

    【讨论】:

    • 那是因为你在别处犯了同样的错误(括号不平衡)。
    • Vazques-Abrams,我将添加到目前为止的代码。在原始帖子的编辑#2 处,给我一分钟。
    • 我只是没看到。我一直在重写它,但它只是不工作哈哈。
    • @ino:现在你在if之前错过了)
    • 最后仍然缺少一个 ) ;应该是:`if options.filename is None and ((options.search_and is None) or (options.search_not is None) or (options.search_start is None) or (options.search_then is None)):
    【解决方案2】:

    这相当于您正在尝试做的事情,并且可能稍微更具可读性

    if options.filename is None and None in [options.search_and, options.search_not, options.search_start, options.search_then]:
      parser.error(usage)
      sys.exit()
    

    【讨论】:

      【解决方案3】:

      if options.filename is None and ((options.search_and is None) or (options.search_not is None) or (options.search_start is None) or (options.search_then is None))

      【讨论】:

      • 那么为什么“@Furbeenator:是的,这就是我写的。?”
      • 当你看到它时,你会扔砖头。
      【解决方案4】:

      在您的原始语句中,“(options.filename is None)”您应该删除最后一个“)”,并将该代码替换为:

       (options.filename is None
      

      这应该可以解决您的语法错误。

      【讨论】:

        【解决方案5】:

        if 语句之前的行

        parser.add_option("-f", "--file", type="string", dest="filename", help="file name"
        

        没有结束语)。

        Python 解释器通常会在下一行抱怨此类语法错误。因此,如果您在任何一行遇到语法错误,建议也检查前一行

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-30
          • 1970-01-01
          相关资源
          最近更新 更多