在赋值之前可能会引用局部变量 - Python

roc*_*uff 2 python encryption function local-variables python-3.x

我一直在尝试制作加密和解密系统,但我遇到了一个小错误.这是我的代码:

    import sys
    import pyperclip


    def copy(data):
        question = input("Copy to clipboard? ")

        if question.lower() == 'yes' or question.lower() == 'y':
            pyperclip.copy(data)
            print("Encrypted message copied to clipboard.")
            rerun()

        elif question.lower() == 'no' or question.lower() == 'n':
            rerun()

        else:
            print("You did not enter a valid input.")
            copy(data)


    def rerun():
        ask = input("\nWould you like to run this program again? ")

        if ask.lower() == "yes" or ask.lower() == "y":
            print(" ")
            run()

        elif ask.lower() == 'no' or ask.lower() == 'n':
            sys.exit("\nThank you!")

        else:
            print("You did not enter a valid input.")
            rerun()


    def encrypt(key, msg):
        encrypted_message = []
        for i, c in enumerate(msg):
            key_c = ord(key[i % len(key)])
            msg_c = ord(c)
            encrypted_message.append(chr((msg_c + key_c) % 127))
        return ''.join(encrypted_message)


    def decrypt(key, encrypted):
        msg = []
        for i, c in enumerate(encrypted):
            key_c = ord(key[i % len(key)])
            enc_c = ord(c)
            msg.append(chr((enc_c - key_c) % 127))
        return ''.join(msg)


    def run():
        function_type = input("Would you like to encrypt or decrypt a message? ")

        if function_type.lower() == "encrypt" or function_type.lower() == "e":
            key = input("\nKey: ")
            msg = input("Message: ")
            data = encrypt(key, msg)
            enc_message = "\nYour encrypted message is: " + data
            print(enc_message)
            copy(data)

        elif function_type.lower() == "decrypt" or function_type.lower() == "d":
            key = input("\nKey: ")

            question = input("Paste encrypted message from clipboard? ")

            if question.lower() == 'yes' or question.lower() == 'y':
                encrypted = pyperclip.paste()
                print("Message: " + encrypted)

            elif question.lower() == 'no' or question.lower() == 'n':
                encrypted = input("Message: ")

            else:
                print("You did not enter a valid input.")
                run()

            decrypted = decrypt(key, encrypted)
            decrypted_message = "\nYour decrypted message is: " + decrypted
            print(decrypted_message)
            copy(decrypted)

        else:
            print("\nYou did not enter a valid input.\n")
            run()

    run()
Run Code Online (Sandbox Code Playgroud)

它表示在分配和突出显示之前可能会引用局部变量'encrypted'

    decrypted = decrypt(key, encrypted)
Run Code Online (Sandbox Code Playgroud)

在run()函数下.

是因为我在其他函数中使用了变量'encrypted'吗?如果是这样,我将如何解决此问题并仍然保持我的程序的功能?

我对python比较陌生,所以如果你能解释一下你的答案我会很感激.

Dee*_*ace 8

如果else执行分支encrypted则不定义.IDE不知道你run()再次打电话.

请记住,这可能会导致无限递归,因此您应该使用另一个控制流机制(尝试使用while在输入有效时中断的循环)


sid*_*d-m 6

分配之前可能会引用局部变量“ encrypted”

是lint生成的警告。

这是因为linter看到encrypted两个if条件内的赋值

 if question.lower() == 'yes' or question.lower() == 'y':
Run Code Online (Sandbox Code Playgroud)

elif question.lower() == 'no' or question.lower() == 'n':
Run Code Online (Sandbox Code Playgroud)

但是,如果条件互为补充,则短毛猫无法知道这两个条件。因此,考虑到所有条件都不为真的情况,变量encrypted将最终未初始化。

为了消除此警告,您可以在任何if带有Nonevalue 的条件之前简单地初始化变量