Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use function variable outside the function in Python?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading