Late Arrivals & List Length


#1

Help! What am I doing wrong?

suitcase = [] 
suitcase.append("sunglasses")

# Your code here!
suitcase.append("bathing suit")
suitcase.append("sunscreen")
suitcase.append("phone")


list_length = [3] # Set this to the length of suitcase

print "There are %d items in the suitcase." % (4)
print suitcase

The error is:
Remember: len(list_name) will return the length of a list as an integer.

In the top right in the console it shows:
There are 4 items in the suitcase.
['sunglasses', 'bathing suit', 'sunscreen', 'phone']

Thanks in advance!


#2

set list_length to the length of suitcases using the len() function, then you use list_length to print the length of your list two lines below


#3

Well. I think that my eyes are burned from the errors... But, it's ok, we all started like that, so why not contribute some help.

You were doing just fine until the variable "list_length"
That's where you messed up. Remember in fist lessons, where we were getting the length of a string? We did this like so: len(string). len(string) will return the number of characters of the string inside the parenthesis. Now, you can do this with lists as well! But, when you type len(list) you will get the number of items in the list! Cool, huh? So, when it asks for the variable list_length, it wants you to type len(suitcase), to get the items in suitcase. The items are now 4, so list_length will be equal to the integer number 4

Then, the print statement uses some advance printing technique we learned in an older lesson:
In that lesson, we used %s to print variables inside strings like "print'I am an %s person' %(adjective)" Where adjective is a variable holding a string.

Now, %d is the same, just used for integers, like the list_length value. So, instead of typing directly "4" inside the parentheses, use list_length.

REMEMBER: Programming is all about elasticity of code, and making things reusable, and that's why putting directly "4" is not entirely wrong, but it's a bad approach if you are a professional programmer, since suitcase's length might change unexpectedly in a complex program

Hope I helped.