分配之前可能会引用局部变量-Python [英] Local variable might be referenced before assignment - Python

查看:350
本文介绍了分配之前可能会引用局部变量-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to make an encryption and decryption system but I have run into a small error. Here is my code:

    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"局部变量可能在赋值之前被引用 并突出显示

It says local variable 'encrypted' might be referenced before assignment and highlights

    decrypted = decrypt(key, encrypted)

在run()函数下.

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

Is it because I used the variable 'encrypted' in other functions? If so, how would I fix this and still maintain the functionality of my program?

我对python来说还比较陌生,所以如果您能解释一下答案,我将不胜感激.

I am relatively new to python so I would appreciate it if you could explain your answers.

推荐答案

在分配之前可能会引用本地变量"encrypted"

local variable 'encrypted' might be referenced before assignment

是短毛猫发出的警告.

这是因为linter认为encrypted被分配了两个if条件内的值

This is because the linter sees that encrypted is assigned values inside two if conditions

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

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

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

however, the linter cannot know that these two if conditions are complementary to each other. So, considering the case when none of the conditions is true, the variable encrypted will end up uninitialized.

要摆脱此警告,只需在任何if条件之前使用None值初始化变量

To get rid of this warning, you can simply initialize the variable before any of the if conditions with None value

这篇关于分配之前可能会引用局部变量-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆