For your hobbies


#1



For your hobbies


Wondering the difference between append and defining a list by each position
Also getting a weird result of entered hobbies (with append or insert) as such [u'golf', u'soccer', u'football']


I expect that hobbies[i] = hobby would assign hobby to the position in the list of i. Such as that hobbies[i] on its first go around the loop would be hobbies[0] = first hobby user enters, second loop around hobbies[i] is now hobbies[1] = second hobby user eneters.
Is this because hobbies[] when declared doesnt have a length yet? Or is the only methods to name a value at a position in a list .append() and .insert(i,hobby) ? I am confused as why this wouldn't work. Thank you for any information


hobbies = []

# Add your code below!
for i in range(3):
    hobby = raw_input("Hobby?: ")
    print i #testing to see what index is
    hobbies[i] = hobby
    #hobbies.append(hobby)
    #hobbies.insert(i,hobby)

print hobbies


#2

You can't assign to a position that doesn't exist.

The u in your string literal is no more weird than the commas, apostrophes and square brackets. The square brackets are part of how a list is represented, the apostrophes and the u is how a unicode string is represented


#3

Right, I thought that would initialize a position. With that said, that limits the options to append() and insert()?

Oh okay, I guess it was strange since in the exercises I've done up to this point I hadn't noticed there being a 'u' within the list of strings. Does this have to do with getting raw_input?


#4

No? Why?

It's only in the text representation of that value, the u's are not in the list, because they aren't values, same as the apostrophes, commas etc aren't values.

Does raw_input return unicode strings? print type(raw_input())
Although I think it's actually supposed to return a byte string, codecademy might have modified it.


#5

I was messing around after I got append to work and was looking for other means/methods to learn.

Yeah it did return
<type 'unicode'>


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.