Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shift for uppercase letters

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

import string

letters = string.ascii_letters #contains 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

#create the Caesar cypher
offset = 5 #choose your shift
totalLetters = 26
keys = {} #use dictionary for letter mapping
invkeys = {} #use dictionary for inverse letter mapping, you could use inverse search from original dict

for index, letter in enumerate(letters):
    # cypher setup
    if index < totalLetters: #lowercase
        letter = letters[index]
        keys[letter] = letters[(index + offset) % 26]
        print(letters[index] + ":" + keys[letter])
    else: #uppercase
        letter = letters.isupper()
        keys[letter] = letters[(index + offset) % 26]
        print(letters[index] + ":" + keys[letter])
       

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

in uppercase condition,

keys[letter] = letters[(index + offset) % 26 + 26]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading