'how to fix my Caesar Cipher function and how the % loop over within the length?
realText = input('please enter a string: ')
step = int(input('please enter step for shifting'))
def caesar_encrypt(realText, step):
outText = []
cryptText = []
uppercase = ['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']
lowercase = ['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']
for eachLetter in realText:
if eachLetter in uppercase:
index = uppercase.index(eachLetter)
crypting = (index + step) % len(uppercase)
cryptText.append(crypting) #list of each crpting index
newLetter = uppercase[crypting]
outText.append(newLetter)
elif eachLetter in lowercase:
index = lowercase.index(eachLetter)
crypting = (index + step) % len(lowercase)
cryptText.append(crypting)
newLetter = lowercase[crypting]
outText.append(newLetter)
print((' ').join(outText))
caesar_encrypt(realText, step)
Input:
BEWARE THE IDES OF MARCH
step: 13
Unexpected output:
O R J N E R G U R V Q R F B S Z N E P U
Expected output:
ORJNER GUR VQRF BS ZNEPU
As my code is shown above, how can I fix this code to get my desired output? and I don't understand how the modulus operators are able to use to loop over the length of a sequence and isn't it for the reminder? can anyone explain more details about that? will be very appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
