i recently found that variables set in a function cannot be used in another function. so to make variables 'universal' i have to make them global variables?

and you do that buy stating them global, like this?

global [VARIABLE NAME]=[VALUE]

i have a feeling there is more to it.

Edited 7 Years Ago by ffs82defxp: n/a

Yes that is the correct syntax for a global variable. How ever you can pass the value of variables around with things like return statements.

Comments
Answered the question directly. Offered an additional option.

There are other options as well.

Make both functions methods of a class, and use class attributes to store relevant data.

Combine both functions into one.

I don't know how to do any of that. lol

can you give me an example script snippet?

Just a general example of functions and their parameters/arguments:

# example of passing parameters/arguments to and from functions

def get_data():
    """get the data from the user, from a file, or hard coded"""
    mydata = [1, 2, 3, 4]
    # return requested data to caller
    return mydata

def process_data(data=None):
    """this is also the main function of the program"""
    # if no data is passed, get it with this function call
    if not data:
        data = get_data()
    minimum_data = min(data)
    maximum_data = max(data)
    # pass the 2 needed data paramters/arguments to this function
    show_result(minimum_data, maximum_data)

def show_result(min_data, max_data):
    """display the result, needs 2 data paramters/arguments"""
    print( "result: min=%s  max=%s" % (min_data, max_data) )

# call the main function to start
# use default data via function get_data()
process_data()

# you can also supply the needed data to the main function
data = list("abcd")
process_data(data)

"""overall result -->
result: min=1  max=4
result: min=a  max=d
"""

Study it carefully! If you have questions, ask. Avoid global variables, as your program gets larger, they can easily create hard to find bugs.

Note: this code will work with Python2 or Python3

Edited 7 Years Ago by Ene Uran: p2 p3

can i get a simpler example like with 3 functions and a simple task/concept

can i get a simpler example like with 3 functions and a simple task/concept

The example is simple and you shoul try to understand it.
Break it down and ask question.

def get_data():
    """get the data from the user, from a file, or hard coded"""
    mydata = [1, 2, 3, 4]
    # return requested data to caller
    return mydata

print get_data()
'''my output-->
[1, 2, 3, 4]
'''

So this is how the first function work.
It take no argument and it has a standar list that get returned out off function.

Then we can look at the next function.

def process_data(data=None):
    """this is also the main function of the program"""
    # if no data is passed, get it with this function call
    if not data:
        #data = get_data()   #here come data from get_data() function.
        data = [1, 2, 3, 4]  #this is the same
    minimum_data = min(data)   #find minium value of a list
    maximum_data = max(data)   #find maxium value of a list
    # pass the 2 needed data paramters/arguments to this function
    #show_result(minimum_data, maximum_data)
    return( "result: min=%s  max=%s" % (minimum_data, maximum_data) )

l = [2,5,5,8]

#with no argument passed to function
print process_data()
'''my output-->
result: min=1  max=4
'''

#here we pass l as an argument
print process_data(l)
'''
my output-->
result: min=2  max=8
'''

Here i have made it so this function work alone,we simualte that data comes from first fuction.
And insted off useing show_result() we return data out off this function.

look at this and try to understan what going on,and ask question if something is unclear.

Edited 7 Years Ago by snippsat: n/a

Here is a rather simple example of a number of functions working together from:
http://www.daniweb.com/forums/showthread.php?p=104853#post104853

def get_name():
    """this function only returns arguments"""
    first = "Fred"
    last = "Ferkel"
    return first, last

def show_name(name):
    """this function only receives an argument"""
    print( name )

def process_name(first, last):
    """this function reveives 2 arguments, returns 1 argument"""
    name = "Mr. " + first +" " + last
    return name

def main():
    """
    this function handles the other functions in order
    and deals with the arguments received and to be passed
    """
    first, last = get_name()
    name = process_name(first, last)
    show_name(name)

# start the program with this function call
main()
Comments
nice example

Okay, i think i understand. Basically you pass a variable from one function to another by 'returning' it ?

And the next functions parameters are supposed to include the variable(s) name(s)?

Okay, i think i understand. Basically you pass a variable from one function to another by 'returning' it ?

And the next functions parameters are supposed to include the variable(s) name(s)?

You pass variables as need as function arguments and/or returns.

This question has already been answered. Start a new discussion instead.