Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with caesar cipher
#1
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()

			
Reply
#2
The return at line 19 is too much indented.

Also note that you forgot â, î, û for french. What about œ and æ ? there are also ë ï ÿ
Reply
#3
Ok thank you very much it work fine now, I only use the most used character in french but yes why not ;)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 2,163 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 1,517 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 1,178 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Where is problem in decrypting ADFGVX cipher Paragoon2 2 1,609 Nov-06-2023, 10:24 AM
Last Post: Paragoon2
  Transposition Cipher Issues beginner_geek07 0 1,728 Apr-08-2022, 07:44 PM
Last Post: beginner_geek07
  Caesar Cipher Help pbrowne 2 3,087 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Learning Python with a Caesar cipher Drone4four 5 10,149 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 4,059 May-16-2020, 10:05 AM
Last Post: DPaul
  cipher decryption tool nightfox82 0 1,863 Mar-25-2020, 06:36 AM
Last Post: nightfox82
  Alternate solutions to cipher problem? Mark17 3 3,339 Oct-08-2019, 01:32 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020