0

我对 python 很陌生,所以这可能有一个非常明显和简单的解决方案,但它是:我创建了一个自定义模块,将其导入另一个脚本 [script2],并希望使用 script2 中定义的局部变量作为导入模块中函数的参数

模块:

def encrypt(inputstr, securitykey = 1, encryptionkey = 1, decryptionkey = 1): #default value of keys is 1

inputstr = str(inputstr)

[converts inputed string into number ids based on encryption key and security key; this is like a 80 line long process, so I won't show it here]

inputstr = int(inputstr)
inputstr = inputstr * decryptionkey * securitykey
inputstr = str(int(inputstr)) #<this is stupid, I know, but for some reason this was the only way it worked

return inputstr

脚本2:

from ModuleDirectory import encryption

msg = input("Enter your message: \n \n")
seckey = input("Enter security key: \n \n")
enckey = input("Enter enrcyption key: \n \n")
deckey = input("Enter decryption key: \n \n")

encrypted = encryption.encrypt(msg,seckey,enckey,deckey)
print(encrypted)

当我这样做时,我得到一个 NameError。如何告诉 Python 我想使用 script2 中定义的变量作为我从模块导入的函数的参数?正如我所说,我对 Python 还很陌生,所以我可能只是个白痴。

4

3 回答 3

0

不确定这是否是您的意思,但您必须使用参数内的变量调用函数才能将实际变量传递给函数。所以最后用参数中的变量调用函数。

例子

encrypt(msg, seckey, enckey, deckey)

但不确定这是否是您的意思。

于 2020-04-29T00:51:06.670 回答
0

def function(String ...) 这里 String 有问题!!请给出正常的字符串变量名,但不要给出字符串!您正在发送一个名为字符串的字符串!因此,您必须与另一个字符串一起获得该字符串!但不是大写字符串!你不能用它作为名字!

于 2020-04-28T23:30:01.023 回答
0

在 python 中,您不必指定字符串,只需将其保留为 var1... 即可。

于 2020-04-28T23:30:45.650 回答