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

Can any one tell how to abs(int(position) work

abs(int(position)

???????

1 Answer

Steven Parker
Steven Parker
230,995 Points

First, you need to balance the parentheses:

abs(int(position))   # <-- note extra ")" was needed

Then, since "position" was assigned by calling "input", it is a string containing what was typed in. So "int" will convert that string into a number that can be used for calculations in a later step.

And just in case the number is negative (such as if the person had typed "-3"), the "abs" function then converts it to the positive equivalent (such as "3" in this example).

So using both functions together converts the input from a string into a number and guarantees that it is positive.

Thanks Steven Parker.