I am writing a program on shifting a word. My desired output should be a:f b:g c:h ... y:d z:e A:F B:G C:H ... Y:D Z:E
JavaScript
x
22
22
1
import string
2
3
letters = string.ascii_letters #contains 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
4
5
#create the Caesar cypher
6
offset = 5 #choose your shift
7
totalLetters = 26
8
keys = {} #use dictionary for letter mapping
9
invkeys = {} #use dictionary for inverse letter mapping, you could use inverse search from original dict
10
11
for index, letter in enumerate(letters):
12
# cypher setup
13
if index < totalLetters: #lowercase
14
letter = letters[index]
15
keys[letter] = letters[(index + offset) % 26]
16
print(letters[index] + ":" + keys[letter])
17
else: #uppercase
18
letter = letters.isupper()
19
keys[letter] = letters[(index + offset) % 26]
20
print(letters[index] + ":" + keys[letter])
21
22
But after running this code, my output is a:f b:g c:h ... y:d z:e A:f B:g C:h ... Y:d Z:e
Seems isupper()
function didn’t work here. Could you help with this based on my code structure. Thanks in advance!
Advertisement
Answer
in uppercase
condition,
keys[letter] = letters[(index + offset) % 26 + 26]