Search code examples
pythonencryptioncaesar-cipher

How do i encrypt the userinput in python?


i am trying to write a code in python, that encrypt the user input. so for example if i write "name" the output should be "obnf". but the problem is that now the output is only "o", so only the first letter is going through the loop and the rest is left out. Any suggestions? here is my code

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

Solution

  • An interator can only be traversed once, then it is exhausted. Use a list instead.

    Consider str.maketrans() and str.translate() for quick substitution.