Hobbies 10/19 BUG?


Codecademy tells me that the code should look like this, which is fine, but when i give the code my answers, the last line where we print the whole list looks like this…

Tell me one of your favorite hobbies: coding
Tell me one of your favorite hobbies: reading
Tell me one of your favorite hobbies: movies
[u’coding’, u’reading’, u’movies’]

Whats with the u’s? Is it a bug?

I thought the last line would look like this instead:

[‘coding’, ‘reading’, ‘movies’]


No. It’s from raw_input() which takes any character input and treats it as unicode. We only see this because the input is appended directly to a list.

hobbies.append(str(raw_input("Enter a hobby: "))

will cure it, but is not necessary. Iterate the list and print, and only the text will print.


Ok, Im not quite familiar with what unicode is…

But we’ve appended the same way in the battelship-project and this didn’t happend…why is that?

That’s what I actually did the first time(and yes it works), but I realised that we already are inputing a string, so the str() is not necessary, and then I checked Codecademy’s code and they didnt use it either.

So what happens now, do I always have to use str() around raw_input everytime I write a program in the future, so that I dont get the u’s or…?

I want to learn everything the right way, thats why I check the Codecademy code before I take notes…stuff like that confuse me so much…


No, that won’t be necessary.

Enter a hobby: abc
Enter a hobby: def
Enter a hobby: ghi
[u'abc', u'def', u'ghi']

The latter is printed in a loop over hobbies.



No matter what I write (when I don’t have str() around raw_input) prints u infront of the words I input…


It is not a bug, but a feature of Python 2. It can be ignored since as we see above, looping over the list prints only the text and not the encoding prefix. If the text happens to contain accented characters they will print correctly.


Ok…so if this happens from time to time, I should just ignore it…so basically forget about str(), and let the u’s popup whenever they want, no need to stress about them…?


They won’t ‘pop up’, and since it is rare that we would print a raw list whole, like we did above, nothing of concern.


But they do pop up here, but they didn’t in the battleship-project with the 5 "O"s…

and why is it rare to print a raw list (not quite sure what you mean with raw)?

What if I create an grocery-app, where I want to input everything I want to buy at the store today, so that the whole list it shows up on the screen when I finish inputing the items I want to buy?


That was not raw input, but code generated string content.

The list would be iterated with a loop and printed to the screen one item per line. We rarely output a list in one statement.

print (grocery_list)

would be,

for item in grocery_list:
    print (item)


You are totally right, by bad!

That part I also get.

But I just got confused about the u’s, you know, why they pop up in the list like they did. But I’ll write down in my notebook to ignore them when they show up like that when we use raw_input in our code.


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