因此,基本上,我正在尝试编写一个程序,在您提供正确的密钥后执行某些操作。
因此,首先我从输入中获取Key后调用Keycheck(Key),然后它通过一个带有key的文件来激怒(每个key在新行("\n")中)。
def Keycheck(Key):
KeyFile = open("keys.key","r", encoding='utf-8')
for line in KeyFile:
line1 = line.strip()
fields = line1.split("\n")
linekey = fields[0]
if Key in linekey:
Keypass = "true"
elif Key != linekey:
continue
很好,但我遇到的问题是将Keypass变量传递给主程序,我尝试阅读有关Kwargs的内容,但我听不懂。
因此,我的问题是如何将Keyword变量从"def Keycheck(Key)"传递到"main program",如果不可能,是否有其他方法可以从文件中已经存储的键中实现键检查?
编辑: 通过主程序我的意思是
def func():
def func2():
Keycheck(Key):
main program:
Key = input("")
Keycheck(Key)
if Keypass == "true":
func()
func2()
else:
break
只是有关我希望程序如何工作的提示:
- 它从输入中获取密钥
- 它调用Keycheck(Key)(或检查密钥是否有效)
- 如果Key位于Keycheck(key)的文件中,它将继续执行程序 如果它在密钥文件中不包含密钥,则只需执行print("wrong key try again")之类的操作,然后重试所有操作
分析解答
如@ Code-Apprentice所述,如果找到密钥,则返回True。否则,返回False循环完成。
def Keycheck(Key):
KeyFile = open("keys.key","r", encoding='utf-8')
for line in KeyFile:
line1 = line.strip()
fields = line1.split("\n")
linekey = fields[0]
if Key in linekey:
return True # found key, return True
return False # loop is done, key not found
# main program
if Keycheck('Key123'):
print('Found Key')
else:
print('Key Not Found')