O'Reilly Forums: Ch 1 Nested Lists Problems? - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ch 1 Nested Lists Problems?

#1 User is offline   09Thorntonm 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 30-December 11

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.
0

#2 User is offline   09Thorntonm 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 30-December 11

Posted 30 December 2011 - 01:54 PM

^ that was supposed to come out indented...
0

#3 User is offline   rb157_13050 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 09-January 12

Posted 09 January 2012 - 02:11 AM

View Post09Thorntonm, on 30 December 2011 - 01:54 PM, said:

^ 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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users