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 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

unpacking.py
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?

nakalkucing
nakalkucing
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! :)

nakalkucing
nakalkucing
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.

Here is what I came up with.

def unpacker(*args):

return args

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

print(val1)

print(val2)

treehouse:~/workspace$ python unpacking.py
Python
rocks!