I have this simple funcion

def newUser():
	print 'Choose a username?'
	Username = raw_input('> ')

The variable 'Username' has to be used by other functions. How can I do it?


Recommended Answers

All 5 Replies

Here is a link to simple on-line tutorial. See the explanation under "Functions". You should book mark this link for future reference http://hetland.org/writing/instant-python.html

you can do:

global Username
Username = raw_input('> ')

in the function, and 'Username' will be available for the rest of the program to use

Using global is not a good soultion at all,learn to give argument at return value out functions.
A simple example.

def newUser():    
    Username = raw_input('Choose a username? ')
    return Username

def reg_user(newUser):
    '''Make 3 users an save to a list'''
    l = []
    for i in range(3):
    return l   
if __name__ == '__main__':     
    print reg_user(newUser)

Choose a username? Tom
Choose a username? Paul
Choose a username? Hans
['Tom', 'Paul', 'Hans']

yeah dont use global, it could be very problematic

def newUser():    
    return raw_input('Choose a username? ')

user = newUser()
# do something to user.

Thank you so much!. I have much to learn ...


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.