Keeping to your original idea, you were fairly close. Note, Python already keeps a handy list of lower case letters:
import string
alphabet = string.ascii_lowercase
message = input('Please insert the message you want to encrypt: ')
key = int(input('What key value do you want in your encryption? '))
output = []
for m in message:
if m in alphabet:
output.append(alphabet[(alphabet.index(m) + key) % (len(alphabet))])
print(''.join(output))
You need to create a new output list of characters as it is not possible to directly change the characters in the original string. This list can then be joined back together to display your output.
So for example, this would give you the following:
Please insert the message you want to encrypt: hello
What key value do you want in your encryption? 3
khoorzruog
Please also note, there are more efficient ways to tackle this, for example the use of maketrans
.
0
solved How do I make a cipher and encrypt the text? [closed]