Reputation: 59
I am trying to encrypt words with a Caesar Cipher technique, but I am not allowed to use def, or Caesar(), or anything of that sort. I am to have an original alphabet in a list, ask the user what they want the shift to be, then create a new alphabet list with that shift. Then, somehow assign each letter in their message with the correct letter from the new alphabet. How do I assign each letter with a new value from the new alphabet? This is how far I've gotten:
originalAlphabet = ['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']
secretAlphabet = []
print("Welcome to the Encryption Generator")
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): "))
userMessage = input("Please, enter a message you'd like to encrypt: ")
userMessage = userMessage.lower()
userMessage = list()
encrypted = []
while userShift not in range(1, 26):
print("Please, enter a valid selection.")
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): "))
for shift in originalAlphabet[0:userShift]:
secretAlphabet = originalAlphabet[userShift:] + originalAlphabet[0:userShift]
print(originalAlphabet)
print(secretAlphabet)
for letter in userMessage:
index = originalAlphabet.find(letter)
newIndex = abs(((userShift-1) - index) - 26)
#for newIndex in secretAlphabet:
# print(encrypted)
Upvotes: 0
Views: 2495
Reputation: 1704
You are very close. In line 8, you redefine userMessage
as an empty list. Just change it to userMessage = list(userMessage)
.
In the last block of your code, you should check if letter is in the original Alphabet. If it is, you should find its index and append the secret alphabet with that index to your encrypted list. Else, you should just append the character (this will make it so punctuation and spaces are kept in your message). At the end, you can join the elements of your encrypted list to make a string.
Input:
originalAlphabet = ['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']
secretAlphabet = []
print("Welcome to the Encryption Generator")
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): "))
userMessage = input("Please, enter a message you'd like to encrypt: ")
userMessage = userMessage.lower()
userMessage = list(userMessage.lower())
encrypted = []
while userShift not in range(1, 26):
print("Please, enter a valid selection.")
userShift = int(input("Please, tell me how much you'd like to shift the alphabet (1-25): "))
for shift in originalAlphabet[0:userShift]:
secretAlphabet = originalAlphabet[userShift:] + originalAlphabet[0:userShift]
print(originalAlphabet)
print(secretAlphabet)
for letter in userMessage:
if letter in originalAlphabet:
idx = originalAlphabet.index(letter)
encrypted.append(secretAlphabet[idx])
else:
encrypted.append(letter)
print(''.join(encrypted))
Upvotes: 0