Python 尝试使用randint生成唯一值并将其分配给字典中的项

Python 尝试使用randint生成唯一值并将其分配给字典中的项,python,dictionary,encryption,Python,Dictionary,Encryption,这段代码应该从字符串中提取每个唯一的字母,然后生成一个唯一的随机数作为其值,然后将其附加在一起。我有唯一的字母部分,但不能生成唯一的随机数。我不知道如何搜索我的字典来查找值是否唯一。同时也为我的代码太草率表示歉意,我对编码还不熟悉 '''This function takes plaintext and then encrypts it''' reference = {} plainText = input("Enter your text for encryption here:\n"

这段代码应该从字符串中提取每个唯一的字母,然后生成一个唯一的随机数作为其值,然后将其附加在一起。我有唯一的字母部分,但不能生成唯一的随机数。我不知道如何搜索我的字典来查找值是否唯一。同时也为我的代码太草率表示歉意,我对编码还不熟悉

    '''This function takes plaintext and then encrypts it'''
reference = {}
plainText = input("Enter your text for encryption here:\n")
counter = 0
while counter < len(plainText):
    try:
        for i in plainText[counter]:
            check = False
            for item in reference.values():
                if item == i:
                    check = True   
            while check == False:
                    value = randint(10,19)
                    if value not in reference:
                        check = True
                        reference.update({i:value})
            counter +=1
    except IndexError:
        print('Error. Please try again\n')
    encryptedText = ""
for j in plainText:
    encryptedText = encryptedText + str(reference[j])
print("Here is your encypted text!\n"+encryptedText)
“”“此函数获取明文,然后对其进行加密”
引用={}
明文=输入(“在此处输入要加密的文本:\n”)
计数器=0
当计数器
如果我明白你想做什么。我建议使用一个集合来获取独特的项目:

text = set(input())
reference = {i: random.randint(10, 99) for i in set}

encrypted = text
for char, valin reference.items():
    encrypted = encrypted.replace(char, str(val))

print(encrypted)
集合保存字符串中的每个唯一项。第2行的dict理解将集合中的每个项目与随机值配对(我选择了10-99,因为它们都是2位数,以便在必要时帮助解密)。for循环以以下方式替换文本输入:它接受引用dict中的每个字符,并用相应的随机值替换它。
仅供参考,
随机
模块不建议用于实际加密,因为它是
伪随机
,很容易被破解。建议将
secrets
模块用于实际应用

某个输入的Expexced输出是什么?举个例子会有帮助