I’m a very new beginner and i don’t understand return function that well and i cant do the third step can someone please help me! this is my code so far and don’t understand the third step - how do i save the returned values to high and low.
You are on the right track. You just need to assign variables to the function get_boundaries (100,20) in order for you to call properly (which is what you defined). And the variables are the values you assigned to the return . Then proceed with print and don’t forget to define your results at the end. I hope I used the right terminology and I hope that makes sense!
Hi,
Is there a way to use one specific returned value from a function that returns several values, without having to save them all to variables?
Eg. if my function returned value1, value2, value3 - how can I use one of those , without having to create 3 variables separated by commas and setting them equal to the function before.
Hi @ringeflange.
You could save them to a single name and simply address the index you know contains the value you want (ideally make it clear to anyone reading your code what you are actually using from the function return)-
this_tuple = my_func()
print(this_tuple[2]) # reference you actually wanted.
It’s also not uncommon to just supply throwaway names for return values you know you don’t need, e.g.
_, _, useful = my_func() # Here _ is used as a throwaway name.
This can be extended with tricks like unpacking, but you’d need to know exactly which values you wanted and you can start to lose readability if it’s abused-
*_, useful = my_func() # For the last element returned.
As the name of the lesson suggests, get_boundaries() has multiple values in the return statement. Python packs the values in a tuple before returning them. (Actually, two or more comma separated values represent a sequence.)
When the return is assigned to comma separated variables, the sequence is unpacked and assigned in the order they were written in the return statement.
So in what you wrote, return gives a set (x,y) nonvisible to the user, where x is a given low_limit and y is a given high_limit. Then (low, high) is set equal to (x,y). And then we individually print low and high to see x and y printed, is that accurate?
Why do we have to do the extra step of assigning the return values to other variables instead of just:
def get_boundaries(target, margin):
low_limit = target - margin
high_limit = target + margin
return low_limit, high_limit
#like this
print(get_boundaries(100,20))
#instead of this what the lesson says
low, high = get_boundaries(100,20)
print (low, high)
As per the instructions you are aiming to save the function output to two variables, low and high. It doesn’t ask you to print any outputs to the console. Whilst you could print them out if you wanted to, should you then neglect to save those variables with the expected names you might not actually pass the testing required to complete the lesson.
In short, assign them to those names because you probably need to.