Hello!
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?
Cheers!

Dani

Recommended Answers

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

[CODE]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):
l.append(newUser())
return l …

Jump to Post

All 5 Replies

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):
        l.append(newUser())        
    return l   
    
if __name__ == '__main__':     
    print reg_user(newUser)

'''-->Out
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 ...

Dani

Be a part of the DaniWeb community

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