Python Forum
Problem with caesar cipher - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with caesar cipher (/thread-8064.html)



Problem with caesar cipher - lucaron - Feb-05-2018

I just write a python's code for change messages with a chosen letter shift, but it only work for the first letter.

Could you explain me what should i change for making it work with all character


ALPHABET = str()
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def main():
    text = input('Entrez le message à chiffrer: ')
    offset = int(input('Entrez le décalage à utiliser:'))
    message = cesar(text,offset)
    print("Le message chiffré est: ", message)

def cesar(text,offset):
    text = convert_text(text)
    new_text = str()
    
    for char in text:
        if char in ALPHABET:
            new_text += swap(char,offset)
        else:
            new_text += char
        return new_text

    

def convert_text(text):  #supprime les caractères spéciaux
    text = text.lower()

    d = {
            "e" :["é","è","ê","ê"],
            "a" :["à"],
            "c" :["ç"],
            "o" :["ô"],
            "u" :["ù"]

        }
    for char in d:
        for c in d[char]:
            text = text.replace (c, char)

    return text.upper()        

def swap(char, offset):
    index = ALPHABET.find(char)
    return ALPHABET [(index + offset) % 26]


if "main" == "main":
    main()

			



RE: Problem with caesar cipher - Gribouillis - Feb-05-2018

The return at line 19 is too much indented.

Also note that you forgot â, î, û for french. What about œ and æ ? there are also ë ï ÿ


RE: Problem with caesar cipher - lucaron - Feb-05-2018

Ok thank you very much it work fine now, I only use the most used character in french but yes why not ;)