Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
for循环后出现Python语法错误(在解释器中)_Python_File_Syntax_Syntax Error - Fatal编程技术网

for循环后出现Python语法错误(在解释器中)

for循环后出现Python语法错误(在解释器中),python,file,syntax,syntax-error,Python,File,Syntax,Syntax Error,我正在从控制台运行一些python代码(粘贴),并得到一个意外的结果。下面是代码的样子: parentfound = False structfound = False instruct = False wordlist = [] fileHandle = open('cont.h') for line in fileHandle: if line is "": print "skipping blank line" continue if "}"

我正在从控制台运行一些python代码(粘贴),并得到一个意外的结果。下面是代码的样子:

parentfound = False
structfound = False
instruct = False
wordlist = []
fileHandle = open('cont.h')
for line in fileHandle:
    if line is "":
        print "skipping blank line"
        continue
    if "}" in line:
        instruct = False
        index = line.index("}")
        wordlist.append(word)
    pass          
try:
    print wordlist
except Exception as e:
    print str(e)
在for循环之后,我想打印
单词列表
。无论我做什么,我都不能包含for循环之外的任何内容。以下是我收到的错误:

...     if "}" in line:
...         instruct = False
...         index = line.index("}")
...         wordlist.append(word)
...     pass          
... try:
  File "<stdin>", line 10
    try:
      ^
SyntaxError: invalid syntax
。。。如果行中有“}”:
...         指示=错误
...         索引=行。索引(“}”)
...         追加(word)
...     通过
... 尝试:
文件“”,第10行
尝试:
^
SyntaxError:无效语法

无论我是手动在终端中键入代码,还是粘贴代码,都会发生这种情况。如果你能提供帮助,我将不胜感激。谢谢大家!

REPL中的
提示意味着它仍然没有完成上一个块。您需要在空行上按Enter键以首先终止它。

它对我有效。您是否可能有缩进错误,在某个地方有额外的空格或制表符?(您的代码中有一些逻辑错误,但至少它可以运行。)
wordlist.append(word)
中的
word
是什么?代码不完整--我在仍然出现错误的情况下尽可能减少了代码,以便更容易找到问题的根源谢谢。我要试一试。如果我将其包含在python文件中,而不是将其输入到解释器中,我还会遇到此错误吗?不会。这种行为是REPL特有的。感谢您的快速响应--这让我发疯了。时间一到,我就接受答案@IgnacioVazquez Abrams这也解决了我的问题-谢谢。这种行为,包括
提示,是否有文档记录?侧边栏:另一个解决方案是使用IPython,它似乎没有这些细微差别。