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 trialZENG ZHIYI
2,097 Points"When catching the values returned from a function, you have to have:" can u give me an example of this quiz?thx
I cant understand this quiz.
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsFrom the quiz Two-ples:
"When catching the values returned from a function, you have to have:..."
"Catching", in this case, means assigning the returned values of a function call to a variable. Some functions return a single object, other functions may return multiple values. When returning multiple values, they are formed into a tuple.
It's an All or One rule. When more than one object is returned from a function, the receiving variables must be a single item, which will get assign the value of the tuple, or the exact number as the number number of objects returned so the return tuple can be properly unpacked.
Example, len()
returns one object, an int
, so exactly one variable can "catch" the output of this function:
length = len("this is a string")
Trying to catch more, throws an error as int
isn't an iterable collection:
In [24]: length = len("this is a string")
In [25]: length, width = len("this is a string")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-1043f5ccb688> in <module>()
----> 1 length, width = len("this is a string")
TypeError: 'int' object is not iterable
In the example below cir_area
returns two values as a tuple. The receiving variables need to be either one or two ("all").
$ ipython
In [1]: import math
In [2]: def cir_area(radius):
...: """Return circumference and area of a circle given the radius."""
...: cir = 2 * math.pi * radius
...: area = math.pi * radius * radius
...: return cir, area # <-- (cir, area) also works here
...:
In [3]: tup1 = cir_area(1)
In [4]: tup1
Out[4]: (6.283185307179586, 3.141592653589793)
In [5]: cur, area = cir_area(1)
In [6]: cur
Out[6]: 6.283185307179586
In [7]: area
Out[7]: 3.141592653589793
So back to the quiz:
Choose the correct answer below:
- A The same number of variables as values
- This works!
- B Just a single variable
- This works!
- C Any number of variables up to the amount of return values (i.e. if 5 values come back, you can use 4 variables and the last will hold two values)
- Not in Python
- D Both A and B
- The Correct Answer
ZENG ZHIYI
2,097 PointsZENG ZHIYI
2,097 PointsIt helps me a lot.thx~