Python Python Basics All Together Now Wrap Up

mohan Abdul
mohan Abdul
Pro Student 972 Points

cant make sense of this code '''elif skit == "Lumberjack": if len(skit) < 3: print("sleeps all day")'''

i can't make sense of this line which has thrown me off, '''' if len(skit) < 3:''' python. can someone help please. here's a snapshot of the code (click on file . I am lost at lines 6 to lines 10. With lines 11 and line 12 , I can't make sense of this line '''print("ni" * len(skit))''

Steven Parker, If you could talk me through reading this code. I would appreciate it.

1 Answer

Steven Parker
Steven Parker
181,132 Points

This is a pretty silly function, but it's just for practice reading code. Here's how I might translate the last part:

    elif skit == "Lumberjack":       # if the argument is "Lumberjack"...
        if len(skit) < 3:            # AND if it has fewer than 3 letters (which is impossible)...
            print("sleeps all day")  # ...then print this 
        else:                        # otherwise, since it has 3 or more letters (actually 10)...
            print("i'm okay")        # THIS will be printed out
    else:                            # finally, if the argument was none of the above...
        print("ni" * len(skit))      # ...print "hi" over and over as many times
                                     #    as the word has letters

Note that when used with a string and a number, the "*" symbol is the repeat operator.
For example:   "Abc" * 4   would become:   "AbcAbcAbcAbc".