【发布时间】:2021-02-03 14:52:31
【问题描述】:
我在尝试调用 def Password() 时遇到 NameError,说“密钥”未定义。当我清楚地在 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!')
【问题讨论】:
-
当我明确定义在RandomKey():函数中时可以,但是如果不是全局变量,
Password函数就看不到了。搜索“python 变量范围”。一种选择是将其作为参数传递给RandomKey函数。 -
你有一个函数
RandomKey,它返回一个值,但你没有显示你在哪里调用了这个函数以及你是否存储了返回的值。
标签: python function return-value nameerror defined