Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functions, Packing, and Unpacking Packing and Unpacking Unpacking Challenge

I'm sure I'm making an obvious mistake on a simple challenge but I don't see where I'm going on with unpacking.

So I'm not sure what I'm doing wrong. From what I can tell I've unpacked it into two variables. TAI

def unpacker(*args):
    return args

val1 = unpacker('Python')
val2 = unpacker('rocks!')

2 Answers

I've been working this one for an hour, and it shouldn't be this difficult. I"m presuming that there is something in the instructions missing or something, as it should be easy. Any help?

12,964 Points

Hi Brian Davis! Sorry it took me so long to get back. Are you still having a problem with this challenge? If so, please post your code below. Happy coding! :)

12,964 Points

Hi William Ray Noble! Good job, you're close. At minute 1:00 in the video Unpacking, Ashley shows a similar example to what this challenge is looking for:

def unpacker():
    return (1, 2, 3)
var1, var2, var2 = unpacker()

With the following explanation:

On the right side of this variable assignment, I'm calling the function, unpacker. Unpacker returns a tuple containing the values 1, 2, and 3. Because the left side of our variable assignment has more than one variable name, Python understands that we're trying to unpack this tuple. Just like passing multiple arguments to multiple parameters is positional, so is unpacking a sequence. In this example, the first element in the tuple 1 will be assigned to var1, ect.

Hope this helps! :) Post back if you need more help.

Brandon Pinnow
Brandon Pinnow
2,446 Points

Here is what I came up with.

def unpacker(*args):

return args

val1, val2 = unpacker('Python', 'rocks!')



treehouse:~/workspace$ python unpacking.py