Jump to content


Photo

Ch 1 Nested Lists Problems?


  • Please log in to reply
2 replies to this topic

#1 09Thorntonm

09Thorntonm

    New Member

  • Members
  • Pip
  • 2 posts

Posted 30 December 2011 - 01:53 PM

hi, i am new to python and probably being really stupid - when i enter the code on pg 25 into IDLE, i get an output which differs to the one in the book :( please help?

>>> movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91, ["Graham Chapman", ["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]

------------------------------------------------------------
>>> for each_item in movies:
if isinstance(each_item, list):
for nested_item in each_item:
if isinstance(nested_item, list):
for deeper_item in nested_item:
print(deeper_item)
else:
print(nested_item)
else:
print(each_item)
------------------------------------------------------------
i was expecting this output:

The Holy Grail
1975
Terry Jones & Terry Gilliam
91
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
------------------------------------------------------------
instead i recieved this:

Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
Terry Jones
['Graham Chapman', ['Michael Palin', 'John Cleese', 'Terry Gilliam', 'Eric Idle', 'Terry Jones']]
------------------------------------------------------------

please help, i have tried everything i could think of. thanks in advance.

#2 09Thorntonm

09Thorntonm

    New Member

  • Members
  • Pip
  • 2 posts

Posted 30 December 2011 - 01:54 PM

^ that was supposed to come out indented...

#3 rb157_13050

rb157_13050

    New Member

  • Members
  • Pip
  • 2 posts

Posted 09 January 2012 - 02:11 AM

^ that was supposed to come out indented...


Can you repost with the indentation? I suspect that is where your problem lies. Your else: print (each_item) needs to be part of the for each_item in movies statement, not one of the later statements.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users