Jump to content


Photo

Chapter 2 - Page 59


  • Please log in to reply
1 reply to this topic

#1 ramcpy

ramcpy

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 June 2013 - 06:52 AM

Hello guys,

I'm stuck on this page. I did not get the logic on how the tabs change levels.

def print_lol(a_list, level):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""

    for each_item in a_list:
        if isinstance(each_item, list):
# increases the level value, in case it is 2, it will be 3 next loop and so on.
            print_lol(each_item, level+1)
        else:
# How it works here? 
#How does it interact with the number of tabs? I really don't get it.
# Where is implicit the number of times that the next level will do?
            for l in range(level):
                print("\t", end='')
            print(each_item)


Thank you!

#2 ramcpy

ramcpy

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 June 2013 - 08:51 AM

Hello guys,

I'm stuck on this page. I did not get the logic on how the tabs change levels.

def print_lol(a_list, level):
    """Prints each item in a list, recursively descending
       into nested lists (if necessary)."""

    for each_item in a_list:
        if isinstance(each_item, list):
# increases the level value, in case it is 2, it will be 3 next loop and so on.
            print_lol(each_item, level+1)
        else:
# How it works here? 
#How does it interact with the number of tabs? I really don't get it.
# Where is implicit the number of times that the next level will do?
            for l in range(level):
                print("\t", end='')
            print(each_item)


Thank you!



lack of attention. I did not realize before that the second for is used by the "tab" print only.

Tks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users