0

Basically I was receiving UnboundLocalError: local variable 'pw_is_long' referenced before assignment and saw using the global keyword helps to get rid of this inside a function. After using it, I am getting NameError: name 'pw_is_long' is not defined

I'm working in selenium and the instagram website. This code worked before I put it inside the function.

My first function:

password = input("Password\n")  
def pw_minimum_length(pw):
        if len(pw) > 5:
            return(True)
        else:
            return(False)
        
pw_is_long = pw_minimum_length(password)

My second and main function:

def login(username, password):
    #some if-else code
    #selenium element selecting & clicking
    #the following is the heirarchy inside the function where I am having this issue
    while (condition):
        if (condition):
            password = input("Password\n")
            global pw_is_long #when this line was not here, I was getting UnboundLocalError
            pw_is_long = pw_minimum_length(password)
        while not(pw_is_long): #line throwing NameError
            print("Password length must be at least 6 characters")
            password = input("Password\n")
            pw_is_long = pw_minimum_length(password)
        

Any answers are helpful. Thanks in advance.

Rohan Shah
  • 479
  • 3
  • 14
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – metatoaster Jul 13 '20 at 01:59
  • If we are just looking at your code, `pw_is_long` is simply won't exist all the time on the instance where the `if` block is not executed; that causes the `NameError`. The `UnboundGlobalError` is simply because `pw_is_long` is also not found at the global scope. – metatoaster Jul 13 '20 at 02:02
  • @metatoaster but since I'm creating ```pw_is_long``` after the first function, ```pw_is_long``` should still be recognized as a variable right? – Rohan Shah Jul 13 '20 at 02:06
  • Can you provide the traceback? – Astik Gabani Jul 13 '20 at 02:10
  • 1
    A name can only be referenced after it has been created, not before. – metatoaster Jul 13 '20 at 02:14
  • good rule: all `global` put at the beginning of function. This way everone will know that you use external variable which can makes problems. If you don't need this variable outside function then better create it at the beginning of function and assign some default value - ie. `pw_is_long = None` – furas Jul 13 '20 at 07:47

1 Answers1

1

If condition is False then it doesn't run if condition and it doesn't create variable pw_is_long = ... but you need it in while not pw_is_long

I see two possibilities:

First: create pw_is_long at start with some default value - ie. None or Flase or 0

def login(username, password):

    pw_is_long = False

    while condition:
        if condition:
            password = input("Password\n")
            pw_is_long = pw_minimum_length(password)
            
        while not pw_is_long:
            print("Password length must be at least 6 characters")
            password = input("Password\n")
            pw_is_long = pw_minimum_length(password)
        

Second: maybe you should rather nested it and run while inside if

def login(username, password):

    while condition:
        if condition:
            password = input("Password\n")
            pw_is_long = pw_minimum_length(password)
            
            while not pw_is_long:
                print("Password length must be at least 6 characters")
                password = input("Password\n")
                pw_is_long = pw_minimum_length(password)
            
furas
  • 134,197
  • 12
  • 106
  • 148