Difference between low, high and low_limit, high_limit?

When naming variables we try to include as much information as can given.

low

high

Those are very general, with not a lot of interpretive information.

low_limit

high_limit

Those are less general, and imply lower and upper bounds. More specific, and easier to rationalize for the reader.

I was a little confused at first by over-complicating something simple. You must define the new variables before they can be used:

def get_boundaries(target, margin):
  low_limit = target - margin
  high_limit = margin + target
  return low_limit, high_limit
  low = low_limit
  high = high_limit
low, high = get_boundaries(100, 20)

-lif

Hello, @tera7835798474.

Welcome to the forums!

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:

low, high = get_boundaries(100, 20)
1 Like

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.

While a function can have multiple return statements. It can only return once.

So in this case it returns the first calculation, then exits.

This is why the example saves the calculations as variables and returns them both in a single return.

1 Like

There are two schools of thought on the subject of return. That much is clear.

One school accepts multiple return branches, the other insists upon one, only. Can we find legitimate arguments for each scenario?

See if we can fill out this question with more information, enough evidence for us to draw our own conclusion. Be sure to link to any sources quoted.

This is a very good explanation, thank you for clearing this up!

I understand how it makes sense but it was very confusing as we have not set parameters in this way so far in the course.

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.

Hello everyone. I was also confused as to why low_limit and high_limit were abbreviated with low and high when we printed the equation.

I decided to change the abbreviation to something else. Bob for low_limit and Mary for high_limit. I printed and got the same output of 120 and 80.

This was my syntax:

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = margin + target
return low_limit, high_limit

bob, mary = get_boundaries(100, 20)
print(bob)
print(mary)

Screen Shot 2020-04-29 at 10.35.37 PM

This made me conclude that the abbreviation (or variable of the variable) is defined by the position within the commas of:

bob (1st position), mary (second position) = get_boundaries(100, 20)

in comparison to the position of the variables low_limit, high_limit within the return line:

return low_limit (1st position), high_limit (2nd position)

Hope this helps!

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.

Why is my code wrong ?

def get_boundaries(target, margin):
low_limit = target - margin
high_limit = target + margin
return low_limit, high_limit
low, high = get_boundaries(100, 20)
return low, high

We cannot see how your blocks are indented, but the last line does not belong. The line above it should not be indented (not inside the block).

you’re meant to save the return value, like this
low = print(low_limit)
high = print(high_limit)

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