Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmohan Abdul
Courses Plus Student 1,453 Pointscant 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 https://w.trhou.se/h7xaqiflpr (click on file basics_end.py) . 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
231,248 PointsThis 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"
.