简体   繁体   English

为什么在出现 IF 条件后会出现“无效语法”错误?

[英]Why am I getting an 'Invalid syntax' error after an IF condition?

I have a Python program with an if condition which checks if an item is NOT in the dictionary and throws an error if it is not.我有一个带有 if 条件的 Python 程序,它检查一个项目是否不在字典中,如果不在则抛出一个错误。 Otherwise the code should keep running.否则代码应该继续运行。 However, I receive an 'invalid syntax' error relating to the next line of code after the if condition and I can't understand why.但是,我在 if 条件之后收到与下一行代码相关的“无效语法”错误,我不明白为什么。 The line of code works fine if run by itself, but not if run together with the if statement.这行代码在单独运行时可以正常工作,但如果与 if 语句一起运行则不行。 I have multiple exceptions in the program so I do not wish to have them and the remaining code all contained in a hierarchy of elif conditions.我在程序中有多个异常,所以我不希望它们和剩余的代码都包含在 elif 条件的层次结构中。 To my understanding, I cannot see anything wrong with this code so I would appreciate if anyone can point out what's causing this.据我了解,我看不出这段代码有什么问题,所以如果有人能指出导致这种情况的原因,我将不胜感激。

Here's an example piece of code which is also producing the same error and the associated error message.这是一段示例代码,它也产生相同的错误和相关的错误消息。

Code sample:代码示例:

item = 'a'
item_dict = {
    'a': ['A1','A2','A3'],
    }

if item not in item_dict:
    raise Exception("Error")

# Extract the relevant add on codes from the dictionary.
item_codes = item_dict.get(item)

Error Message:错误信息:

File "<stdin>", line 5
item_codes = item_dict.get(item)
         ^
SyntaxError: invalid syntax

EDIT: I have fixed typos in the example code and still receive the same syntax error.编辑:我在示例代码中修正了拼写错误,但仍然收到相同的语法错误。 Note, that the error only occurs if I run this block of code as one.请注意,只有当我将此代码块作为一个运行时才会发生错误。 If I run it all, excluding the bottom line which assigns the item_codes variable and then run that line by itself afterwards it is fine and produces the expected results, but the code I'm writing requires being run as one whole program.如果我运行它,不包括分配 item_codes 变量的底线,然后自己运行该行,它会很好并产生预期的结果,但我正在编写的代码需要作为一个完整的程序运行。

You didn't give it a newline after the if block.你没有在 if 块之后给它换行。 I did not change anything else and it is working.我没有改变任何其他东西,它正在工作。

item = 'a'
item_dict = {'a': ['A1','A2','A3']}

if item not in item_dict:
    raise Exception("Error")

item_codes = item_dict.get(item)

look at this看这个

>>> if item not in item_dict:
...   print("hi")
... print("hhi")
  File "<stdin>", line 3
    print("hhi")
        ^
SyntaxError: invalid syntax

In the cmd the statement after if is being treated as a part of that if , and so it is expecting a tab.在 cmd 中, if 之后的语句被视为 if 的一部分,因此它需要一个制表符。

You need to add a new line , thats it.您需要添加一个新行,就是这样。

The only way i can reproduce your error of syntax is if i leave off a closing brace on the previous line for example我可以重现您的语法错误的唯一方法是,例如,如果我在上一行省略了一个右大括号

item = 'a'
items_dict = {
    'a': ['A1','A2','A3'],
    }
if item not in items_dict:
    raise Exception("Error"   #here i have left off the closing parenthesis

item_codes = items_dict.get(item)

#ERROR#
  File "<ipython-input-13-919d98974d80>", line 5
    item_codes = items_dict.get(item)
             ^
SyntaxError: invalid syntax

double check the lines before you code to see if you have missed a closing brace or qoute or something在编码之前仔细检查行,看看是否错过了右大括号或 qoute 之类的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM