4

I wrote a couple of functions to calculate the NPS and Margin of error of a sample responses.

I don't want to return the result from first function and then passing it to another function to be able to use them.

So I was looking to create global variables which can be available outside the function it's created so that it can be used in other function without having to pass them.

But it seems to throw the error. Any idea how to achieve this? I don't want to use a Class and make these variables as Class variables.

def nps_score(responses): 
    """Function to get the NPS score from the 
     Survey responses 

    """
    global sample_size = len(responses)
    global promoters_proportion = sum([1 for x in responses if x >=9])/sample_size
    global detractors_proprotion= sum([1 for x in responses if x<=6])/sample_size

    global sample_NPS= promoters_proportion - detractors_proportion

    print("Sample Net Promoter Score(NPS) is {} or {}%".format(sample_NPS,sample_NPS*100))



def moe():
    """ Calculate the margin of error
    of the sample NPS 

    """

    # variance/standard deviation of the sample NPS using 
    # Discrete random variable variance calculation

    sample_variance= (1-sample_NPS)^2*promoters_proportion + (-1-sample_NPS)^2*detractors_proportion

    sample_sd= sqrt(sample_variance)

    # Standard Error of sample distribution

    standard_error= sample_sd/sqrt(sample_size)

    #Marging of Error (MOE) for 95% Confidence level
    moe= 1.96* standard_error

    print("Margin of Error for sample_NPS of {}% for 95% Confidence Level is: {}%".format(sample_NPS*100,moe*100))
5
  • Have you somehow not heard of return values by now? You've been writing Python for at least 5 years. Commented Dec 10, 2018 at 23:51
  • 3
    Wait, reading that again, you say you don't want to return the values. Why not? return is much less likely to cause problems than global variables. Commented Dec 10, 2018 at 23:53
  • I don't want to return values and then save it another variables only to be passed in moe function. Commented Dec 11, 2018 at 0:19
  • You can also use function attributes, e. g. after you define your nps_score function, you can add nps_score.sample_size = 0, then you can address that as nps_score.sample_size both inside and outside nps_score. Not saying that you should, though. Commented Dec 11, 2018 at 2:10
  • "I don't want to return values and then save it another variables only to be passed in moe function." If it's going to be immediately passed to another function, and isn't useful for anything else, then there is no need for an intermediate variable: another_function(first_function()). However, in general, it's not clear why you wouldn't want to do these assignments. Anything else will be at least as much work. Global variables (if they aren't constant) make it harder to reason about the program logic. Commented Jul 27, 2022 at 19:36

1 Answer 1

9

You have to declare the variable to be global, then use it. Like so:

def add_to_outside():
    global outside #say that it is global
    outside = 1 #now create it!

def see_it():
    global outside #say that it is global
    print(outside)

##As shown:
add_to_outside()
see_it()
#output: 1

The keyword global at the start makes all variables of that name in the function reference the global values. You don't say a variable is global and change it in the same statement.

Also, only put the global keyword at the start of the function. It doesn’t need to be next to the changes to the variables, and is only needed once.

To declare multiple variables global, do it like this:

global var1, var2, var3 #etc.
Sign up to request clarification or add additional context in comments.

4 Comments

This is when u declare variable outside function. I want to use a variable declared inside a function to be used in another function
global is the only way without using return. Just have one function call reference the global variable that another function changed. If you want the "created in the function" behavior just don't create the variable at the start, e.g. don't use the outside = 0 line. I have updated my example to show that.
ok got it. issue is dont change in the same statment. But do i still need to declare it as global in see_it() function when it has been declared already in upper fun
@Baktaawar if you want to change the value, then yes. There are exceptions (like if you just want to read the value), but you should just always do it or you may get unwanted behavior.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.