如何在python中加密用户输入?

时间:2019-11-15 16:54:46

标签: python encryption caesar-cipher

我正在尝试用python编写代码来加密用户输入。 因此,例如,如果我写“名称” ,则输出应为“ obnf” 。但是问题在于,现在的输出仅为“ o” ,因此只有第一个字母通过循环,其余的字母被忽略了。 有什么建议么? 这是我的代码

userInput = input("write something: ")


letters = iter(["A","B","C","D", "E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","X","Y","Z","0","1","2","3","4","5","6","7","8","9","a","b","c", "d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"])


def Encoding(user_input):
    encrypted_msg = ""
    for i in range(len(user_input)):
        char = user_input[i]
        if char in letters:
            encrypted_msg += next(letters)
    return encrypted_msg

print(Encoding(userInput))

2 个答案:

答案 0 :(得分:1)

一个插入器只能被遍历一次,然后耗尽。请改用list

考虑使用str.maketrans()str.translate()进行快速替换。

答案 1 :(得分:-1)

下面的代码解决了这个问题:

checkout payapl

干杯!