How to use function variable outside the function in Python?

Question:

I’m new to Python. My codes are similar to this example:

banana = ps.Series(x)

def chocolate(co):
    co=math.sqrt(co)
    if co > 10:
       milk = co - 5
    else:
       milk = co + 5

and i want to take the milk value to be calculated again outside the function like this:

chocolate(banana)
banana_milk=banana.pow(milk)

I’ve tried it and got NameError: name 'milk' is not defined. How to fix it? should i use class? if i use class, i still don’t get where to put the ‘milk’ definition

Please don’t ask me to do all outside the function. My code is more complicated than the example.

Asked By: irene G

||

Answers:

banana = ps.Series(x)

milk = 0

def chocolate(co):
    global milk
    co=math.sqrt(co)
    if co > 10:
       milk = co - 5
    else:
       milk = co + 5
Answered By: nigel239
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.