jeudi 29 octobre 2015

Using len with get() function giving incorrect statistics?

I have some code which asks the user to input 26 characters to make their own encryption code and if it's 26 characters long and is not the alphabet or the preset encryption_code, the code will change the encryption_code to whatever they've entered. import tkinter from tkinter import * from tkinter import ttk from tkinter.ttk import *

encryption_code = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'

window = tkinter.Tk()    
window.title("Encryption/Decryption")

change_frame = tkinter.Frame(window)
changed_frame = tkinter.Frame(window)

encrypt_entry = tkinter.Entry(change_frame)

def code_change():
    global changed_frame
    global encrypt_entry
    print(len(encrypt_entry.get()))
    if encrypt_entry.get() == 'LFWOAYUISVKMNXPBDCRJTQEGHZ' or 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        encrypt_entry.delete(0, tkinter.END)
        changed_incorrect.configure(background=window.cget('bg'))
        changed_incorrect.config(text="Please enter a different code", foreground='red')
        changed_incorrect.pack()
        changed_frame.pack()

    if len(encrypt_entry.get()) == 26:
        encryption_code = encrypt_entry.get()
        encrypt_entry.delete(0, tkinter.END)
        changed_label.configure(background=window.cget('bg'))
        changed_label.config(text="You have successfully changed the encryption code!")
        change_header.config(text=str(encryption_code))
        changed_incorrect.pack_forget()
        changed_label.pack()
        changed_frame.pack()

    elif len(encrypt_entry.get()) < 26 or len(encrypt_entry.get()) > 26:
        changed_incorrect.configure(background=window.cget('bg'))
        changed_incorrect.config(text="Please enter only 26 characters", foreground='red')
        changed_label.pack_forget()
        changed_incorrect.pack()
        changed_frame.pack()

change_label = tkinter.Label(change_frame, text="Please enter your own encryption code in block capitals", font=('Helvetica', 12))
change_header = tkinter.Label(change_frame, text="Make sure it is all 26 letters and do not repeat a letter to prevent errors", font=('Helvetica', 12))
change_confirm = ttk.Button(change_frame, text="Confirm", width=20,  command=code_change)

changed_label = tkinter.Label(changed_frame, text="You have successfully changed the encryption code!", font=('Helvetica', 14))
changed_incorrect = tkinter.Label(changed_frame, text="Please enter your code again", font=('Helvetica', 14))

change_label.pack()
change_header.pack()
encrypt_entry.pack()
change_confirm.pack()
change_frame.pack()

window.mainloop()

My problem is whenever I try and input 26 characters e.g. QWERTYUIOPASDFGHJKLZXCVBNM, it tells me I haven't entered 26 characters when it clearly is 26 characters and the message is only meant to pop up if the user hasn't entered 26 characters.

UPDATE: using print(len(encrypt_entry.get())) has shown me that my entry is 26 but my code is saying it's not 26 characters.

Aucun commentaire:

Enregistrer un commentaire