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 464 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 650 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 493 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Where is problem in decrypting ADFGVX cipher Paragoon2 2 662 Nov-06-2023, 10:24 AM
Last Post: Paragoon2
  Caesar Cipher Help pbrowne 2 2,184 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Learning Python with a Caesar cipher Drone4four 5 4,828 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Coding caesar's cipher drewbty 3 2,778 May-16-2020, 10:05 AM
Last Post: DPaul
  Alternate solutions to cipher problem? Mark17 3 2,420 Oct-08-2019, 01:32 PM
Last Post: jefsummers
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 5,887 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,397 May-15-2018, 04:28 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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