Jump to content


Photo

Chapter 1 - Meet Python - If Else Problem


  • Please log in to reply
1 reply to this topic

#1 tarundurga

tarundurga

    New Member

  • Members
  • Pip
  • 1 posts

Posted 23 March 2011 - 06:25 PM

I'm trying to get the code for IF ELSE to work... and it's fine till 'else' appears and then it gives me an error-

for each_item in movies:
	if isinstance (each_item, list):
		for nested_item in each_item:
			print(nested_item)
			else:

SyntaxError: invalid syntax

Any idea why this is happening?

Consequently, I also tried playing with
dir(_builtins_)
in the python / IDLE shell and it gave me the following error-

Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
dir(_builtins_)
NameError: name '_builtins_' is not defined

I'm stuck! :blink: Please Haaaalllllppppp!

#2 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 23 March 2011 - 10:14 PM

The "builtins" name has two underscores before and after, so it's __builtins__ not _builtins_.

WRT your use of "else:", note that your indentation is off... the "else:" statement has to be indented at the same level as the "if" it is associated with. Check out the code on page 28 for an example.

Hope this helps.

--Paul.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users