Why can't I print variables inside the function? (Return multiple values)

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

print(low,high)

So okay,this works as it should, but just out of curiosity my question is why can’t I print low_limit and high_limit instead of low and high like this: print(low_limit,high_limit) ? I get : NameError: name ‘low_limit’ is not defined. I want to understand the logic behind this. Thank you

Edit: I think I got my answer in the next exercise. The variables defined in the function exist only in the function,am I right ?

Hi, @arc6017287897 Yes, you are correct. It becomes more clear if you preserve indentation by using the </> icon in the menu bar atop the text box you are typing in:

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

low,high=get_boundaries(100,20)
print(low,high)

The variables low_limit and high_limit are defined in the namespace of the function, and are not visible (available) outside of the function

1 Like