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 trialYoussef Moustahib
7,779 PointsCan you fix my code?
So I tried to do the new style of shopping list slightly different from Kenneth's. The problem I am having is follows:
1) When I add a new item (anything after my first added item) it prompts me to know where I would like to put it, if I press enter to send it to the back of the list, it does not append to the list.
2) The insert method works, once I am prompted for a position, I will give it position 1 for example, but it won't move to position 1? it will be 2nd in the list.
Here is my code, please look at "test1.py":
Thank you!
2 Answers
Steven Parker
231,248 Points- When you don't enter a value, an exception occurs during the attempt to convert it into a number. The exception code sets "position" to 0 but never adds the item to the list.
- The number given to "insert" is not the number to insert at, but to insert after. The code you have in "shoppinglist3.py" handles this correctly by subtracting one from the value, but "test1.py" doesn't do that.
Youssef Moustahib
7,779 PointsHi Steven Parker,
1) If it sets it to "0" then shouldn't it append it to the start of the list then?
2) Could you please breakdown why position -1 appends it to the right place?
Jennifer Nordell Please also take a look if you have time, thanks
Steven Parker
231,248 Points- The exception code sets the "position", but then it never does an "append" or an "insert" — as the code is now, those are only done in the "else" conditions.
- Another (and more common) way to think about the argument to "insert" is that inserts in front of the item based on index number. And index numbers start at 0 instead of 1. So if you count your positions starting from 1 you nust subtract one to get the index number.