我在一个IF条件之后为什么会得到一个’无效的语法’错误?

huangapple go评论117阅读模式
英文:

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

问题

我有一个Python程序,其中有一个if条件,它检查字典中是否没有某个项目,如果没有则引发错误。否则,代码应该继续运行。然而,我收到一个与if条件后的下一行代码相关的"无效语法"错误,我无法理解为什么会出现这个错误。如果单独运行该行代码,它可以正常工作,但如果与if语句一起运行,则会出现问题。我的程序中有多个异常,因此我不希望它们和剩余的代码都包含在一系列elif条件中。根据我的理解,我看不出这段代码有什么问题,所以我会感激任何人能指出是什么原因导致了这个问题。

以下是一个示例代码片段,也产生了相同的错误和相关的错误消息。

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

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

# 从字典中提取相关的附加代码。
item_codes = item_dict.get(item)

错误消息:

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

编辑:我已经在示例代码中修复了拼写错误,仍然收到相同的语法错误。请注意,只有当我将这个代码块作为一个整体运行时才会出现错误。如果我运行所有代码,但不包括底部分配item_codes变量的那一行,然后单独运行该行,那么一切都正常,会产生预期的结果,但我正在编写的代码需要作为一个整体程序运行。

英文:

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. 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. The line of code works fine if run by itself, but not if run together with the if statement. 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. 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 = &#39;a&#39;
item_dict = {
    &#39;a&#39;: [&#39;A1&#39;,&#39;A2&#39;,&#39;A3&#39;],
    }

if item not in item_dict:
    raise Exception(&quot;Error&quot;)
    
# Extract the relevant add on codes from the dictionary.
item_codes = item_dict.get(item)

Error Message:

File &quot;&lt;stdin&gt;&quot;, 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.

答案1

得分: 1

你在if块后面没有换行。
我没有改变其他任何东西,它正在工作。

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

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

item_codes = item_dict.get(item)

看这个

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

在cmd中,if后面的语句被视为if的一部分,所以它期望有一个制表符。

你需要添加一个新的行,就这样。

英文:

You didn't give it a newline after the if block.
I did not change anything else and it is working.

item = &#39;a&#39;
item_dict = {&#39;a&#39;: [&#39;A1&#39;,&#39;A2&#39;,&#39;A3&#39;]}

if item not in item_dict:
    raise Exception(&quot;Error&quot;)

item_codes = item_dict.get(item)

look at this

&gt;&gt;&gt; if item not in item_dict:
...   print(&quot;hi&quot;)
... print(&quot;hhi&quot;)
  File &quot;&lt;stdin&gt;&quot;, line 3
    print(&quot;hhi&quot;)
        ^
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.

You need to add a new line , thats it.

答案2

得分: 1

我能重现你的语法错误的唯一方式是如果我在前一行遗漏了一个闭括号,例如:

item = 'a'
items_dict = {
    'a': ['A1','A2','A3'],
    }
if item not in items_dict:
    raise Exception("Error"   #这里我遗漏了闭括号

item_codes = items_dict.get(item)

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

在编写代码之前仔细检查前面的行,看看是否遗漏了闭括号、引号或其他内容。

英文:

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 = &#39;a&#39;
items_dict = {
    &#39;a&#39;: [&#39;A1&#39;,&#39;A2&#39;,&#39;A3&#39;],
    }
if item not in items_dict:
    raise Exception(&quot;Error&quot;   #here i have left off the closing parenthesis
                    
item_codes = items_dict.get(item)

#ERROR#
  File &quot;&lt;ipython-input-13-919d98974d80&gt;&quot;, 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

huangapple
  • 本文由 发表于 2020年1月3日 18:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定