簡體   English   中英

在賦值之前可能會引用局部變量 - Python

[英]Local variable might be referenced before assignment - 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()

and highlights 它表示和突出顯示

    decrypted = decrypt(key, encrypted)

在run()函數下。

是因為我在其他函數中使用了變量'encrypted'嗎? 如果是這樣,我將如何解決此問題並仍然保持我的程序的功能?

我對python比較陌生,所以如果你能解釋一下你的答案我會很感激。

如果執行else分支,則不定義encrypted IDE不知道您再次調用run()

請記住,這可能會導致無限遞歸,因此您應該使用另一個控制流機制(嘗試使用在輸入有效時中斷的while循環)

在分配之前可能會引用局部變量'encrypted'

是一個由linter產生的警告。

這是因為linter看到encrypted的值是在兩個if條件下分配的值

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

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

然而,如果條件相互補充,短絨不能知道這兩個。 因此,考慮到沒有條件成立的情況, encrypted變量將最終未初始化。

要消除此警告,您可以在任何具有None值的if條件之前簡單地初始化變量

run()之前添加encrypted = None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM