Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialZack Parr
4,091 PointsHow can I enter items without adding quote marks?
Greetings Treehouse mates,
I've been writing some of the code featured in these videos outside of the workspace, creating files with a text editor, and using my computer's terminal. I've been staying true to the shopping list code featured here in the video, however it seems that if I want to enter an item, I have to enter it with quote marks. Other than that it works perfectly fine. Can you help me modify my code so I can enter any item without quotemarks? here's what I have so far.
# make a list to hold on to our items
shopping_list = []
# print out instructions on how to use the app
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
# ask for new items
new_item = input("> ")
# I thought converting the input variable to a string would solve the problem.
new_item = str(new_item)
# be able to quit the app
if new_item == 'DONE':
break
# add new items to our list
shopping_list.append(new_item)
# print out the list
print("Here's your list:")
for item in shopping_list:
print(item)
HERE'S WHAT MY SHELL DISPLAYS WHEN I TRY TO ENTER apples WITHOUT QUOTEMARKS:
Traceback (most recent call last): File "/Users/NAMEWITHHELD/Documents/Programming/Treehouse/shopping_list.py", line 10, in <module> new_item = input("> ") File "<string>", line 1, in <module> NameError: name 'apples' is not defined
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsIn Python 2.7, input()
evaluates the input as code, so strings need to be quoted. Py2.7 has raw_input()
that treats all input as strings (no quotes needed).
In Py3.x, the Py2.7 raw_input()
was renamed input()
and the Py2.7 input()
functionality was replaced by eval(input())
So switch to raw_input()
or to Py3.x
Zack Parr
4,091 PointsThanks, man
ashitaachuthan
2,169 PointsAgree with Chris. I'm using Python 2.7 and raw_input was the only way to avoid adding the dreaded quote marks to the input() function.