NameError: name 'Key' is not defined

NameError: name 'Key' is not defined

我在尝试调用 def Password() 时遇到 NameError,提示 'Key' 未定义。当我清楚地在 RandomKey(): 函数中定义它并返回值时...我该如何修复它?

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!')

当然Key没有定义,因为它在其他函数中是私有变量。您可以使用 1000 种变体来修复它。在第一个尝试这个

Key = RandomKey()
if pass_code == Key