Using len with get() function giving incorrect statistics? Using len with get() function giving incorrect statistics? tkinter tkinter

Using len with get() function giving incorrect statistics?


The issue is in the first if statement -

if encrypt_entry.get() == 'LFWOAYUISVKMNXPBDCRJTQEGHZ' or 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':

This is always True, since the grouping of boolean expressions makes it -

if (encrypt_entry.get() == 'LFWOAYUISVKMNXPBDCRJTQEGHZ') or ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):

And all non-empty strings are True in boolean context. This causes the program to delete whatever is in the entry, and hence you get the error from second if condition. You can instead do -

encryptgetted = encrypt_entry.get()if encryptgetted == 'LFWOAYUISVKMNXPBDCRJTQEGHZ' or encryptgetted == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':


You have a couple of problems. First is this statement:

if encrypt_entry.get() == 'LFWOAYUISVKMNXPBDCRJTQEGHZ' or 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':

When you use an expression like if x == "a" or "b", it's the same as if you did if (x == "a") or ("b"), which is the same as if (x == "a") or ("b" != "").

Since the second condition will always be true in your code if the entry field is not empty, this whole condition will always be true. So, that block of code is getting executed for everything except the case where the entry field is blank.

The second problem is that inside this first "if" you are deleting everything in the entry widget. Since every "if" block is re-fetching the contents, all of the following "if" statements will be getting an empty string.

The correct way to code this is to get the value exactly once at the start of your program, then make proper use of else so that you only go through one block of code:

user_input = encrypt_entry.get()if (user_input == 'LFWOAYUISVKMNXPBDCRJTQEGHZ' or     user_input == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'):    ...elif (len(user_input) == 26):    ...else    ...