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 trialTara Edwards
6,521 PointsGetting "EOL while scanning string literal" error, and what happened when I tried to fix it.
Hi:
I am going over projects and code challenges and adding them to my portfolio of work.
When I did Shopping List Take Three, I noticed that I got a "EOL while scanning string literal" error. I decided to try and make the input statement all one line.
One problem: when I did, the other functions acted odd. I printed the new items list, and all items were there. When in the while loop, only the last item in the list is added. If I had a list "coffee, eggs, Snickers", when I typed SHOW, I would get a list of three Snickers.
So, how do I fix the "EOL while scanning string literal" error without the message looking messy. Second, what am I doing that is making the script add the last item only?
Edit: I noticed one error, not updating what variable I would use. I corrected that. However, I cannot see if it worked due to "EOL while scanning string literal" error.
Here is my code:
shopping_list = list()
def show_help():
print("\nSeparate each item with a comma. ")
print("Enter DONE when you are finished adding to the list, SHOW to see the current items in the list, and HELP for this message. ")
def show_list():
count = 1
for new_item in shopping_list:
print("{} : {}".format(count, new_item))
count += 1
#I realize this is not how most Python developers handle this #problem. I kept it in to show what I knew at the time of the course
print("Give me a list of things that you want to buy at the grocery store: ")
show_help()
while True:
new_stuff = input("Your list? ")
if new_stuff == "DONE":
print("\nHere is your list: ")
show_list()
break
elif new_stuff == "HELP":
show_help()
continue
elif new_stuff == "SHOW":
show_list()
continue
else:
new_list = new_stuff.split(',')
#for n in new_list:
# print(n)
#OK, I thought it was putting spaces in the list
#However, when I did that, I still got the right amount, wrong item problem
index = input("Add this in a certain spot? Press ENTER for the end of the list.
"Or give me a number for the placeholder on the list.
"Currently we have {} items in our list. ").format(len(shopping_list)))
if index:
try:
spot = int(index) - 1
except:
print("That number is not in our list. ")
continue
#Using the try/except statement just had the last item added three times.
spot = int(index) - 1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
else:
for item in new_list:
shopping_list.append(item.strip())
#So it fills the list with the last time of the list
#Has there even been a list made? Need to check on new_list
[edit: formtting --cf]
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou appear to be missing end quotes:
index = input("Add this in a certain spot? Press ENTER for the end of the list." #<-- missing end quote
"Or give me a number for the placeholder on the list." #<-- missing end quote
"Currently we have {} items in our list. ".format(len(shopping_list))) #<-- extra paren removed
Tara Edwards
6,521 PointsThanks, that worked great.
Andrew Rodrigues
6,159 PointsI also noticed that using input() is bad practice and creates many exceptions and errors. I always use raw_input() and convert the string to whatever value if I need to later. This whole script gave me errors, until I replaced the input() with raw_input()! I don't know why they are teaching with input()...
Chris Freeman
Treehouse Moderator 68,441 PointsGood point. In Python 2, raw_input()
is suggested in the docs under input()
. From Python 2 docs input():
Consider using the raw_input() function for general input from users.
However, in Python 3, input()
replaced raw_input()
. Treehouse is focusing on Python 3, hence it teaches input()
.
Alex Thomas
4,247 PointsAlex Thomas
4,247 PointsAppease Master Control and all is forgiven.