gpt4 book ai didi

python - 我有一个字典 Python 2.7 的错误

转载 作者:行者123 更新时间:2023-11-28 21:49:07 26 4
gpt4 key购买 nike

我制作的词典将字母替换为短划线 (-) 和两个数字。在我的字典里,“t”=“-21”。当我从字典中调用“t”时,我得到“-21”。但是当我调用 replace_all(text, dic) 时,输出是“-2--5-63-5-63”知道出了什么问题吗?

Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
>>> def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
#Example A
>>> var = "t"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-2--5-63-5-63'
>>> ldic3["t"]
'-21'
#Example B
>>> var = "a"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-02'
>>>

快速编辑:

这是为了让文本不可读,通过一遍又一遍地改变它。在不破坏我的程序的情况下,我不能将我的字符串变成它自己的字典。这是为了一次更改整个段落中的字符。

此外,“replace_all(text, dic)”的代码是我在网上找到的,我不确定它是如何工作的。

这是我的程序:

from Tkinter import *
import time

def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text

passchar = {"a":"0","b":"1","c":"2","d":"3","e":"0","f":"1","g":"2","h":"3","i":"0","j":"1","k":"2","l":"3","m":"0","n":"1","o":"2","p":"3","q":"0","r":"1","s":"2","t":"3","u":"0","v":"1","w":"2","x":"3","y":"0","z":"1"," ":""}
ldic0 = {"a":"-82","b":"-93","c":"-11","d":"-91","e":"-88","f":"-27","g":"-01","h":"-17","i":"-72","j":"-90","k":"-99","l":"-42","m":"-21","n":"-12","o":"-81","p":"-28","q":"-73","r":"-96","s":"-92","t":"-87","u":"-86","v":"-62","w":"-00","x":"-31","y":"-69","z":"-77","A":"-02","B":"-09","C":"-43","D":"-10","E":"-46","F":"-78","G":"-13","H":"-24","I":"-35","J":"-46","K":"-05","L":"-83","M":"-96","N":"-70","O":"-98","P":"-48","Q":"-29","R":"-32","S":"-52","T":"-94","U":"-53","V":"-66","W":"-44","X":"-51","Y":"-71","Z":"-80","0":"-97","1":"-39","2":"-26","3":"-54","4":"-22","5":"-07","6":"-40","7":"-30","8":"-50","9":"-60"}
ldic1 = {"a":"-01","b":"-02","c":"-03","d":"-04","e":"-05","f":"-06","g":"-07","h":"-08","i":"-09","j":"-10","k":"-11","l":"-12","m":"-13","n":"-14","o":"-15","p":"-16","q":"-17","r":"-18","s":"-19","t":"-20","u":"-21","v":"-22","w":"-23","x":"-24","y":"-25","z":"-26","A":"-27","B":"-28","C":"-29","D":"-30","E":"-31","F":"-32","G":"-33","H":"-34","I":"-35","J":"-36","K":"-37","L":"-38","M":"-39","N":"-40","O":"-41","P":"-42","Q":"-43","R":"-44","S":"-45","T":"-46","U":"-47","V":"-48","W":"-49","X":"-50","y":"-51","Z":"-52","0":"-53","1":"-54","2":"-55","3":"-56","4":"-57","5":"-58","6":"-59","7":"-60","8":"-61","9":"-62"}
ldic2 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
#ldic3 = {"a":"-62","b":"-61","c":"-60","d":"-59","e":"-58","f":"-57","g":"-56","h":"-55","i":"-54","j":"-53","k":"-52","l":"-51","m":"-50","n":"-49","o":"-48","p":"-47","q":"-46","r":"-45","s":"-44","t":"-43","u":"-42","v":"-41","w":"-40","x":"-39","y":"-38","z":"-37","A":"-36","B":"-35","C":"-34","D":"-33","E":"-32","F":"-31","G":"-30","H":"-29","I":"-28","J":"-27","K":"-26","L":"-25","M":"-24","N":"-23","O":"-22","P":"-21","Q":"-20","R":"-19","S":"-18","T":"-17","U":"-16","V":"-15","W":"-14","X":"-13","Y":"-12","Z":"-10","0":"-09","1":"-08","2":"-07","3":"-06","4","-05","5":"-04","6":"-03","7":"-02","8":"-01","9":"-00"}

ndic0 = {"0":"3","1":"4","2":"1","3":"7","4":"9","5":"8","6":"2","7":"5","8":"6","9":"0"}
ndic1 = {"0":"1","1":"4","2":"3","3":"5","4":"0","5":"8","6":"2","7":"6","8":"9","9":"7"}
ndic2 = {"0":"2","1":"9","2":"8","3":"4","4":"7","5":"3","6":"0","7":"1","8":"6","9":"5"}
ndic3 = {"0":"4","1":"2","2":"3","3":"9","4":"0","5":"1","6":"7","7":"8","8":"6","9":"5"}


class Application(Frame):

def __init__(self, master):
#Initialize the frame
Frame.__init__(self,master)
self.grid()
self.create_widgets()

def create_widgets(self):
self.instruction = Label(self, text = "ENCRYPTION KEY:")
self.instruction.grid(row = 0, column = 1, columnspan = 2, sticky = N) #sticky = direction W = West

self.key = Entry(self) #Promps for user input
self.key.grid(row = 1, column = 2, sticky = N)

#SPACER1
self.spacer1 = Label(self, text = " ")
self.spacer1.grid(row = 2, column = 0, columnspan = 2, sticky = N)

#-------------
#ENCRYPT BUTTON
self.encrypt_button = Button(self, width = 18, height = 2, text ="ENCRYPT", command = self.textboxa1)
self.encrypt_button.grid(row = 3, column = 0, sticky = N)

#CLEAR BUTTON
self.clear_button = Button(self, text ="CLEAR", width = 18, command = self.clearboxes)
self.clear_button.grid(row = 3, column = 2, sticky = N)

#DECRYPT BUTTON
self.decrypt_button = Button(self, width = 18, height = 2, text ="DECRYPT",) #command = self.textboxa2
self.decrypt_button.grid(row = 3, column = 3, sticky = N)

#Top TextBox
self.textbox1 = Text(self, width = 50, height = 15, wrap = WORD) # wrap = WORD CHAR or NONE
self.textbox1.grid(row = 4, column = 0, columnspan = 4, sticky = N)

#-------------
#SPACER2
self.spacer2 = Label(self, text = " ")
self.spacer2.grid(row = 5, column = 0, columnspan = 2, sticky = N)

#Bottom TextBox
self.textbox2 = Text(self, width = 50, height = 15, wrap = WORD)
self.textbox2.grid(row = 6, column = 0, columnspan = 4, sticky = N)


def textboxa1(self):
keyvar = self.key.get()
textvar = self.textbox1.get(0.0, END)
keyvarl = len(keyvar)
if keyvarl > 25 or keyvarl < 3:
self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, '"' + keyvar + '"' + " -ERROR PASSWORD MUST BE 3 - 25 CHARACTERS LONG")
else:
step = 0
num = 0
passcode = replace_all(keyvar, passchar)
#self.textbox2.delete(0.0, END)
#self.textbox2.insert(0.0, "start")
for i in range(keyvarl):
char = list(passcode)
dig = passcode[num]
self.textbox2.insert(0.0, "")
if dig == "0":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
else:
textvar2 = replace_all(textvar2, ndic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
elif dig == "1":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
else:
textvar2 = replace_all(textvar2, ndic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
elif dig == "2":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
textvar2 = replace_all(textvar2, ndic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
else:
textvar2 = replace_all(textvar2, ndic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
keyvar2 = keyvarl - 1
if i == keyvar2:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
else:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
self.textbox2.insert(0.0, keyvar2)
self.textbox2.insert(0.0, " of ")
self.textbox2.insert(0.0, num)
self.textbox2.insert(0.0, "Step: ")
# self.textbox2.insert(0.0, textvar2 + textvar)
num = num + 1



# def textboxa2(self):
# input1 = self.key.get()
# input2 = self.textbox1.get(0.0, END)

# self.textbox2.delete(0.0, END)
# self.textbox2.insert(0.0, input1 + input2)

def clearboxes(self):
self.textbox1.delete(0.0, END)
self.textbox2.delete(0.0, END)









root = Tk()
root.title("FirePie Demo - Message Encryptor!")
root.geometry("403x630")
app = Application(root)

root.mainloop()

最佳答案

“t”替换为“-21”

然后“1”被替换为“-55”

然后两个“5”都被“-59”替换,结果是“-2--59-59”

然后将两个“9”都替换为“-63”,得到返回的“-2--5-63-5-63”。

如果您不想替换之前替换的字母之一,请考虑一次替换一个字母:

def replace_all(text, dic):
return ''.join(dic.get(x, x) for x in text)

get 中的第二个 x 是默认值,以防 dict 没有键 x .

关于python - 我有一个字典 Python 2.7 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com