3

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!

3
  • It works for me. Is it possible you have an indentation error, with extra spaces or tabs somewhere? (There are some logic errors in your code, but at least it runs.) Commented Aug 30, 2012 at 2:01
  • what is word in wordlist.append(word)? Commented Aug 30, 2012 at 2:03
  • The code isn't complete -- I pared down as much as I could while still getting an error so it'd be easier to get to the root of the problem Commented Aug 30, 2012 at 2:04

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I'll give that a shot. If I include this in a python file, instead of typing it into the interpreter, will I still encounter this error?
No. This behavior is peculiar to the REPL.
Thank you for the quick response -- that was driving me crazy. I'll accept the answer as soon as the timer is up!
@IgnacioVazquez-Abrams This also solved my problem too - thanks. Is this behavior, including the ... prompt, documented somewhere? I must have missed it.
Sidebar: another solution is to use IPython, which doesn't have these nuances, it seems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.