I formatted your code, so we can see how you had originally indented it. Please review this post: How do I format code in my posts?
Regarding your code, these lines will never be executed:
Consider what return does. There is also no need for those lines to be executed. Values assigned to variable inside of a function have local scope (are only accessible inside the function). The values that are returned by the function are âunpackedâ and assigned to low and high in this line:
You are correct the variables inside a function are local to that function unless shenanigans are engaged, but that is outside the scope of this question.
If youâre curious thereâs a good discussion of it over on stack overflow:
It has several commented code examples to help get the finer points across.
I found it much more intuitive to remain consistent with the variable naming scheme. I also added a print() line to check my work which seemed to vindicate my approach. Let me know if this doesnât make sense to anyone else as I am pretty new to this stuff.
def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target
return low_limit, high_limit
# Two vairables areassigned to the same function call thereby returning two different values/results.
low, high = get_boundaries(100, 20)
# A print statement of the above removes the confusion
print(low, high)
Thanks, this helped me, I was clueless as to how I got my answer when the variable names do not match up, but itâs not about the variable names. You are calling the return values in order and the names do not have to match up, which confused me at first but seemed like the only explainable way for my programs results.
@mtfâs response about checking indentation and a moving a particular line outside the function block would be correct. In your code youâre using print as a function, which will print to the console, but it will return None to the names low and high.
Maybe I need more coffee, and Iâm sure the answer is simple, but, I still have this question. Why save the values globally(?) when you can plug in any two numbers in the print() function?
Assuming I opened the right lesson I think is the one of the few examples where youâre asked to return the values rather than just pritnting them at any point. I guess the point is to show how you might start unpacking from a function with multiple returns. Nothing wrong with printing them too but if the lesson asks them to be returned and referenced by specific names it might not pass any built-in tests to check for those names and the equivalence of their contents to the testing values.
I believe your question refers to the calls in your print statements? While we may be able to see the returns on the screen, we cannot utilize those values since they are gone from memory.