Search code examples
pythonfunctionreturn-valuenameerrordefined

NameError: name 'Key' is not defined


I am getting a NameError, when trying to call in def Password() saying that 'Key' is not defined. When clearly I have defined it in the RandomKey(): function and returned the value... How do I fix it?

def RandomKey():

            Main_Key = random.randint(1,25) 
            Key = Main_Key
            return Key 

def Password():
            while T:
                    pass_code = (int(input('Enter The Key: '))) 

                    if pass_code == Key and User_Menu_Input == 2:
                          time.sleep(.3) 
                          print('Key recognized... Transfering to the Encryption Menu') 
                          Timer(Time)
                          ReadFile() 
                          break
                    elif pass_code == Key and User_Menu_Input == 3:
                          time.sleep(.3) 
                          print('Key Correct... Transfering to Decryption Menu') 
                          Timer(Time) 
                          Decrypt() 
                          break

                    else:
                            print('Key Not Recognized... Try Again!')

Solution

  • Of course Key is not defined in reason it's private var in other function. You can fix it with 1000 variations. In first one try this

    Key = RandomKey()
    if pass_code == Key