Home>

I am creating a program to set a user name and password.

I want to use a variable defined inside a function outside the function, but I get an error.

In this code, it corresponds to user_name and password.

How can I use these variables outside the function?

I thought that I could use these variables because they were executed before they were handled outside the function. I would be happy if you could answer why.

def input_user_name ():
    while True:
        user_name = input ("Please enter your name:")
        if user_name:
            print ("Are your name \" {} \ "?". format (user_name))
            user_name_ok = input ("Yes or No:")
            if user_name_ok == "Yes":
                print ("Save")
                return user_name
            elif user_name_ok == "No":
                print ("Please enter the correct name")
                print ("")
                continue
            else:
                print ("Enter yes or no")
                continue
        else:
            print ("")
            continue
def input_password ():
    print ("")
    print ("Set password")
    while True:
        print ("")
        print ("password must be an integer with at least 6 characters")
        print ("")
        password = input ("your password:")
        password = str (password)
        if password and len (password)>= 6:
            try:
                password = int (password)
            except ValueError:
                print ("")
                print ("Please enter an integer")
                continue
            print ("Please enter again for confirmation")
            print ("")
            password_a = input ("your password:")
            try:
                password_a = int (password_a)
            except ValueError:
                print ("")
                print ("Please enter an integer")
                continue
            if password == password_a:
                print ("Save")
                return password
            elif password! = password_a:
                print ("password entered first time and passwrd entered second time are different")
                print ("Please input again from the first time")
                continue
            else:
                pass
        elif password and len (password)<6:
            print ("Please enter an integer of at least 6 characters")
            continue
        else:
            print ("Please enter password")
            pass
def InConclusion ():
    user_information = {"name": user_name, "password": password}
    print ("")
    print (user_information)
def user_start ():
    user_name = input_user_name ()
    password = input_password ()
    InConclusion ()
print (user_start ())

I examined the function to find out the scope of processing within the function, but I didn't understand.

Append

I corrected it, but NameError occurred.
I would be glad if you could teach me what was the problem.

Error code ↓

Traceback (most recent call last):
  File "<stdin>", line 1, in<module>
  File "<stdin>", line 4, in user_start
  File "<stdin>", line 2, in InConclusion
NameError: name 'user_name' is not defined
Append

Thanks for your answers.
I understood the globalization of variables. I will try to write more code to use it when needed.
I understood somehow about the scope. I'm fishing the site.

I'd like to ask you a little, but isn't it better to ask this level of questions?
I thought of what was on the hot question.

We will decide the best answer for the time being.

  • Answer # 1

    Use the

    argument.

    def InConclusion (user_name_, password_):
        user_information = {"name": user_name
    _, "password": password_}
        print ("")
        print (user_information)
    def user_start ():
        user_name = input_user_name ()
        password = input_password ()
        InConclusion (user_name, password)

    InConclusion argument variable names have an underscore at the bottom to make them aware that they are dummy arguments.

  • Answer # 2

    A variable defined within a function is only valid within that function
    If you want to make it valid outside of a function, make it a global variable

  • Answer # 3

    There is a memory I wrote earlier, "Please check the difference between local and global variables, scope, etc."・ ・

    Variables bound in functions are not visible

    Basically, functions interact with the outside with arguments and return values ​​