I'm trying to create a for loop that prompts the user for a hobby 3 times, then appends each one to hobbies.
Here's what I've come up with so far:
hobbies = []
for tries in range(3):
hobby = raw_input(input("what's your favorite hobby?: "))
hobbies.append(hobby)
After I enter a response at the user input prompt, let's say for example my response is "competitive eating", I get the following error in Terminal:
Traceback (most recent call last):
File "hobbyprompt.py", line 12, in <module>
hobby = raw_input(input("what's your favorite hobby?: "))
File "<string>", line 1, in <module>
NameError: name 'competitive eating' is not defined
I'm sure I'm making a really stupid mistake here, but I can't seem to figure out what I'm doing wrong.
input
is equivalent to eval(raw_input)
. It's completely redundant in your example. Just drop it and keep the raw_input
only:
hobby = raw_input("what's your favorite hobby?: ")
EDIT:
To answer the question in the comments, input
takes the string and attempts to evaluate it as a python expression (see eval
's documentation for details). Since "competitive eating" is not variable you've already defined, it cannot be evaluated, and hence the NameError
.