Local variable might be referenced before assignment - Python

前端 未结 3 1104
你的背包
你的背包 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: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

提交回复
热议问题