Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I ran this code in the Python terminal:

>>> for news in news_list:
...     print news("li", {"class", "first"})[0].a["href"]
...     
...     

I want to be able to close this for loop in the terminal, but I have no idea how to. I tried pressing Enter twice but it doesn't work.

share|improve this question
    
You are missing a closing brace. Can you explain what you are trying to do in the print statement? –  Karthik Dec 10 '12 at 5:50
    
Sorry the brace should have been there. I'm looping through an array that has html data in it. It works, I just don't know how to close the loop in the terminal. –  jason328 Dec 10 '12 at 5:55
    
It seems you are using some nested Data Structures. I can't tell what is the problem without looking at the whole code. Please attach the whole code. –  Karthik Dec 10 '12 at 5:59
3  
enter twice works for me. Make sure the 2nd enter is untabbed –  Karthik T Dec 10 '12 at 6:05
1  
I see this is a console. Can you do print news and print news_list before that and give us the output? We'll get an idea of the datastructures there. –  SiddharthaRT Dec 10 '12 at 10:02
add comment

1 Answer

It doesn't matter what data he is working on. Either way you're just using print "spam" or some complicated expression, the for-loop should be closed after two enters or a Python exception will be raised either with bad data or a syntax error.

The problem is you're indenting the empty lines, so the interpreter thinks you're still going to give it some code.

See the difference in below code snippet (select the text to see why):

>>> for x in range(4):
...     print x
... 
0
1
2
3
>>> for x in range(4):
...     print x
...     
...     
...     
... 
0
1
2
3
>>> 
share|improve this answer
    
An aside: Check out bpython. It is a command-line python interpreter with keyword suggestions, auto indentation, memory of history across sessions, an option to save code to a file, etc. The first two features make it a killer app. So many times I've pressed tab in the default interpreter only to find a tab on the screen! –  S Prasanth Dec 11 '12 at 6:33
    
@SPrasanth, you mean ipython? –  warwaruk Dec 12 '12 at 9:23
    
@warwaruk No, bpython apps.ubuntu.com/cat/applications/bpython Will look into ipython though. –  S Prasanth Dec 12 '12 at 10:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.