for 循环后的 Python 语法错误(在解释器中) [英] Python syntax error (in the interpreter) after a for loop

查看:43
本文介绍了for 循环后的 Python 语法错误(在解释器中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从控制台运行一些 Python 代码(粘贴进来),但得到了意想不到的结果.代码如下所示:

I'm running some python code (pasted in) from the console, and getting an unexpected result. Here's what the code looks like:

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 循环之后,我想打印 wordlist.无论我做什么,我都不能在 for 循环之外包含任何内容.这是我收到的错误:

After the for loop, I'd like to print the wordlist. No matter what I do, I can't include anything outside the for loop. Here's the error I receive:

...     if "}" in line:
...         instruct = False
...         index = line.index("}")
...         wordlist.append(word)
...     pass          
... try:
  File "<stdin>", line 10
    try:
      ^
SyntaxError: invalid syntax

无论我是在终端中手动输入代码还是粘贴代码,都会发生这种情况.如果您能提供任何帮助,我将不胜感激.谢谢!

It occurs whether I type the code by hand into the terminal or if I paste it in. I'd appreciate any help you can offer. Thank you!

推荐答案

REPL 中的 ... 提示意味着它还没有完成上一个块.您需要先在空行上按 Enter 来终止它.

The ... prompt in the REPL means that it still hasn't finished the previous block. You will need to press Enter on an empty line to terminate it first.

这篇关于for 循环后的 Python 语法错误(在解释器中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆