Odd output using while loops


#1

With my own code and the solution codeacademy gives me, i’m getting weird outputs

Code:
hobbies = []

for num in range(3):
hobby = raw_input("Tell me one of your favorite hobbies: ")
hobbies.append(hobby)

print hobbies

Output:
Tell me one of your favorite hobbies: baseball
Tell me one of your favorite hobbies: Tell me one of your favorite hobbies: [u’baseball’, u’baseball’, u’baseball’]

It accepts the first response but then does the rest itself? Then it appends the one answer three times?

An explanation would be appreciated greatly


#2

looks fine, maybe a glitch in the lesson? What if you run your code at repl?


#3

yep, works perfectly fine on repl

It might be because the version of chrome is super old, so I tried it on another laptop with the newest version of Microsoft Edge

New problem. Exact same code, but it still adds the letter “u” before each string in the list. So i get for example:

[u’baseball’, u’swimming’, u’biking’ ]

Weeeiiirddd


#4

might be a problem in the lesson, there have been problems with raw_input some time ago, but i never got the feeling this bug went away for 100%

the u is of unicode, see the docs:

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


#5

I see

Soooo, no way of getting rid of it?


#6

there is almost always a way.

from the docs:

Unicode strings are expressed as instances of the unicode type, one of Python’s repertoire of built-in types.

string is also built-in type, python allows converting from one type to the other.


#7

I had a similar problem. I think it’s something related to codecademy because I got it to work fine using Jupyter Notebook.

Here’s my codecademy code (Without the indents–sorry they don’t show up on this blog):

hobbies = []

for i in range(3):
hobby = raw_input("Enter a hobby: ")
hobbies.append(hobby)
print hobbies

Here’s my codecademy output:

Enter a hobby: cooking
[u’cooking’]
Enter a hobby: [u’cooking’, u’cooking’]
Enter a hobby: [u’cooking’, u’cooking’, u’cooking’]

The output repeatedly appends the first input and doesn’t let the user enter anything after the first input. It seems to input the appended list as new input.

Here’s the same code as entered into Jupyter Notebook (Without the indents–sorry they don’t show up on this blog):

hobbies = []

for i in range(3):
hobby = raw_input("Enter a hobby: ")
hobbies.append(hobby)
print hobbies

Here’s the Jupyter Notebook output:

Enter a hobby: cooking
[‘cooking’]
Enter a hobby: soccer
[‘cooking’, ‘soccer’]
Enter a hobby: reading
[‘cooking’, ‘soccer’, ‘reading’]

This output makes more sense.


#8

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