局部变量可能在赋值前被引用 \- Python

新手上路,请多包涵

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

     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()

它说 局部变量’encrypted’可能在分配和突出显示之前被引用

    decrypted = decrypt(key, encrypted)

在 run() 函数下。

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

我对 python 比较陌生,所以如果你能解释你的答案,我将不胜感激。

原文由 rocketsnstuff 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.7k
2 个回答

局部变量“encrypted”可能在赋值前被引用

是 linter 生成的警告。

这是因为 linter 看到 encrypted 在两个 if 条件下被赋值

 if question.lower() == 'yes' or question.lower() == 'y':

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

然而,linter 无法知道这两个 if 条件是互补的。因此,考虑到当所有条件都不为真时,变量 encrypted 将最终未初始化。

要消除此警告,您可以简单地在任何 if 条件之前初始化变量 None

原文由 sid-m 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果 else 分支被执行,则 encrypted 未定义。 IDE 不知道您再次调用 run()

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

原文由 DeepSpace 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题