Local variable might be referenced before assignment - Python

前端 未结 3 1074
你的背包
你的背包 2021-01-14 20:59

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


    d         


        
相关标签:
3条回答
  • 2021-01-14 21:23

    Before run() add encrypted = None

    0 讨论(0)
  • 2021-01-14 21:36

    local variable 'encrypted' might be referenced before assignment

    is a warning generated by the linter.

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

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

    and

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

    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.

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

    0 讨论(0)
  • 2021-01-14 21:38

    if the else branch is executed then encrypted is not defined. The IDE doesn't know you call run() again.

    Keep in mind this might lead to an infinite recursion so you should be using another control flow mechanism (try using a while loop that breaks when the input is valid)

    0 讨论(0)
提交回复
热议问题