Hobbies 10/19 BUG?


#1

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’]


#2

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.


#3

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…


#4

No, that won’t be necessary.

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

The latter is printed in a loop over hobbies.

https://docs.python.org/2.7/howto/unicode.html


#5

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


#6

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.


#7

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…?


#8

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.


#9

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?


#10

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)

#11

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.


#13

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