3

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))
Baktaawar
  • 7,086
  • 24
  • 81
  • 149
  • Have you somehow not heard of return values by now? You've been writing Python for at least 5 years. – user2357112 Dec 10 '18 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. – user2357112 Dec 10 '18 at 23:53
  • I don't want to return values and then save it another variables only to be passed in moe function. – Baktaawar Dec 11 '18 at 00: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. – Headcrab Dec 11 '18 at 02: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. – Karl Knechtel Jul 27 '22 at 19:36

1 Answers1

8

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.
Eb946207
  • 748
  • 8
  • 26
  • This is when u declare variable outside function. I want to use a variable declared inside a function to be used in another function – Baktaawar Dec 11 '18 at 00:19
  • `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. – Eb946207 Dec 11 '18 at 00:21
  • 1
    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 Dec 11 '18 at 00:25
  • @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. – Eb946207 Dec 11 '18 at 00:28