views:

113

answers:

3
def Input():
    c = raw_input ('Enter data1,data2: ')
    data = c.split(',')
    return data

I need to use list data in other functions, but I don't want to enter raw_input everytime. How I can make data like a global static in c++ and put it everywhere where it needed?

+3  A: 

Add a single line to your function:

def Input():
    global data
    c = raw_input ('Enter data1,data2: ')
    data = c.split(',')
    return data

The global data statement is a declaration that makes data a global variable. After calling Input() you will be able to refer to data in other functions.

Greg Hewgill
If data is global do you need the line `return data` ?
Lord British
@Lord British: Nope.
THC4k
+2  A: 

using global variables is usually considered bad practice. It's better to use proper object orientation and wrap 'data' in a proper class / object, e.g.

class Questionaire(object):
    def __init__(self):
        self.data = ''

    def input(self):
        c = raw_input('Enter data1, data2:')
        self.data = c.split(',')

    def results(self):
        print "You entered", self.data

q = Questionaire()
q.input()
q.results()
Ivo van der Wijk
A: 

If data is global do you need the line return data ? – Lord British 1 hour ago "yes bcoz u cont call data directly i think

prasa