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 trial
Freddy Venegas
1,226 PointsShopping list take 3 - double input
When I add the code to check for a ValueError I get double inputs of the same item. For example, I have this
while True:
new_stuff = raw_input("> ")
if new_stuff == 'DONE':
print("\nHere's 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(",")
index = raw_input("Add this at a certain spot? Press enter for the end of the list, " "or give me a number. Currently {} items in the list. ".format(len(shopping_list)))
try:
if index:
spot = int(index) -1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
except ValueError:
print("That is not a number.")
else:
for item in new_list:
shopping_list.append(item.strip())
if I enter bananas > pick any spot that's not enter > then enter SHOW I see the same item I just entered twice. And yes, even entering 0 adds the item twice. Like this:
1. bananas
2. bananas
Any ideas?
1 Answer
Freddy Venegas
1,226 PointsSeems like this did the trick, by moving try: & except: inside if index: - please respond if this is the best solution.
if index:
try:
spot = int(index) -1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
except ValueError:
print("This is not a number.")
else:
for item in new_list:
shopping_list.append(item.strip())