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

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

问题描述

我从控制台运行一些python代码(粘贴),并获得了意想不到的结果。这是代码如下所示:

  parentfound = False 
structfound = False
instruct = False
wordlist = []
fileHandle = open('cont.h')
fileHandle中的行:
如果行为:
打印跳过空行
继续
如果}行:
instruct = False
index = line.index(})
wordlist.append(word)
通过
尝试:
打印wordlist
除了异常作为e:
打印str(e)

在for循环之后,我想打印 wordlist 。不管我做什么,我不能在for循环之外添加任何东西。这是我收到的错误:

  ...如果}行:
... instruct = False
... index = line.index(})
... wordlist.append(word)
... pass
... try:
文件< stdin>,第10行
try:
^
语法错误:无效语法

无论我是将代码手动输入到终端还是将其粘贴,我都会感谢您提供的任何帮助。谢谢!

解决方案

REPL中的 ... 提示它仍然没有完成以前的块。您将需要在空行上按输入以终止它。


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)

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!

解决方案

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天全站免登陆