使用len和get()函数给出错误的统计信息? [英] Using len with get() function giving incorrect statistics?

查看:183
本文介绍了使用len和get()函数给出错误的统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码要求用户输入26个字符来制作自己的加密代码,如果长度为26个字符且不是字母或预设 encryption_code ,代码会将 encryption_code 更改为他们输入的内容。

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()

我的问题是每当我尝试输入26个字符时,例如QWERTYUIOPASDFGHJKLZXCVBNM,它告诉我当它显然是26个字符时我没有输入26个字符,如果用户没有输入26个字符,则该信息只是弹出。

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:使用print(len(encrypt_entry.get()))向我显示我的条目是26,但是我的代码说它不是26个字符。

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.

推荐答案

问题出在第一个 if 语句 -

The issue is in the first if statement -

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

这总是 True ,因为布尔表达式的分组使它成为 -

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

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

并且所有非空字符串在布尔上下文中都是 True 。这会导致程序删除条目中的任何内容,因此如果条件,则会从第二个中获得错误。你可以改为 -

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':

这篇关于使用len和get()函数给出错误的统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆