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

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

Ali Dahud
Ali Dahud
3,459 Points

I don't understand the position = None part and the position - 1 part.

I don't understand where in the video he says

position=None

and

(position-1, item)

2 Answers

Hi Ali,

I'm fairly new to Python but I'll give you my best understanding. Let's start with your first inquiry...

  1. Why do we use 'position = None'?

In Python 'None' is how the language recognizes a "null value" or a "nontype" object. Thanks to the line bit of code "position = abs(int(position))" we are able to handle any number that might be negative. However, if someone enters a float or unrecognizable type it will automatically set it to a "Null" value or in this case "None".

  1. What does (position-1, item) mean?

At this point in the code we've passed all the 'try' and 'except' blocks and we are ready to find a spot on our shopping list for our 'item'. What this line does is try to find the requested spot in the list and we subtract 1 from the number entered because a numbered list starts at 1 but indexes in lists start at 0 (Zero). For example, if I wanted to put my 'Milk' at the number 3 spot in my list it would need to go into index[2]. But we have to assume that the user doesn't know this so we subtract 1 from their number so it will be in the spot they desire.

I hope this helped make sense of this, happy coding! :)

Ali Dahud
Ali Dahud
3,459 Points

Thank you very much we need you here! Don't go anywhere :)

Jan Durcak
Jan Durcak
12,662 Points

where did you get this kind of knowledge, do you have any references or it is from treehouse?

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The line previous

position = abs(int(position))

is key. An attempt is first made force the submitted value to be zero or positive. If this fails for some reason, like a letter was submitted, the position is set to None to signify no valid value was entered.

position - 1 is used to change from the 1-based counting used by the user to the 0-based counting used by Python container indexes.

Post back if you need more help. Good luck!!!

Ali Dahud
Ali Dahud
3,459 Points

I understand this but why can you explain? I mean why did he write position=None. And why, was it set to counting from 1 previously? and where?

Ali

Swan The Human
seal-mask
.a{fill-rule:evenodd;}techdegree
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

Hi could you explain the abs for me? im not sure that was really covered so im confused at what its purpose is and why its specifically being used here. thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The built-in function abs() returns the absolute value of the argument. That is, it returns the positive value if the argument is negative (-5 becomes 5). If the argument is zero or positive, then it is returned unchanged.