For your hobbies


#1
for hobby in range(3):
    hobby = raw_input("What is your hobby?") 
    hobbies.append(hobby)
    print hobbies

When I put this code in, I get a weird output.

What is your hobby? roller derby
[u'roller derby']
What is your hobby? cooking
[u'roller derby', u'cooking']
What is your hobby? eating
[u'roller derby', u'cooking', u'eating']
None

How can I get rid of the 'u' before every hobby listed?


#2

Mmm, try something like this, maybe?

print ", ".join(hobbies)

Instead of

print hobbies

The u there is not an error, by the way (in case you were thinking that).


#3

I just used "print hobby" instead of "print Hobbies" and that worked fine! :slightly_smiling:


#4

That u just means that it is a Unicode string. Here's how to remove them:

Reference:

Call str() on a unicode string to create a regular string. String literals with a u prepended e.g. u'string' are unicode strings.

Example:

a = u'hello'
=> a
u'hello'
=> str(a)
'hello'

Credits to Rafe Kettler


#5

I wrapped my definition of hobby variable in str() in order to get rid of the unicode prefix.

for i in range(3):
hobby = str(raw_input("Name one of your hobbies: "))
hobbies.append(hobby)

print (hobbies)

Result after running

Name one of your hobbies: guitar
Name one of your hobbies: sports
Name one of your hobbies: gym
['guitar', 'sports', 'gym']
None


#6

From what I've seen it seems there is a bug in this module.

My very first attempt at this I typed it out exactly how I thought it should be, running into troubles I went to the forum to find what I was missing, NOTHING is the answer yet I still got syntax errors. after resetting the code and entering the EXACT same code, it worked.

Try resetting the code and writing it all over, it seems to work, not sure why it ran into errors the first time.


#7

I see nothing wrong with it.