1

I'm trying to create a simple Caesar Cipher function in Python that shifts letters based on input from the user and creates a final, new string at the end. Problem is that the final cipher text shows only the last shifted character, not an entire string with all the shifted characters and when the letter z for example the program doesn't restart at the beginning of the alphabet PS i am french educated so some lines might be in french Here's my code:

list=list()
r=0
choix=int(input("Veuillez entrer 1 pour coder ou 2 pour decoder"))
if choix==1 :
    cle=int(input("Quelle est votre clé?"))
    phrase=input("Quelle est votre phrase? ")
    for i in phrase :
        r=ord(i)
        r=(r+cle)%26
        lettre=chr(r)
        list.append(lettre)
    print(list)
elif choix==2 :
    cle=int(input("Quelle est votre clé?"))
    phrase=input("Quelle est votre phrase? ")
    for i in phrase :
       r=ord(i)
       r=(r-cle)%26
       lettre=chr(r)
       list.append(lettre)
    print(list)
4
  • Tu ne devrais pas utiliser list comme nom de variable. En effet, il s'agit d'un nom déjà utilisé comme nom de fonction et de type de données. Commented Jan 29, 2018 at 21:58
  • You are print(lettre) instead of list for encoding. You're overriding the builtin list with your own list. list=list.append(lettre) sets list to None. It should just be list.append(lettre). See the dupe target. Commented Jan 29, 2018 at 22:11
  • Your code is riddled with issues. You need to break down the problem and ensure you can do one part at a time. Commented Jan 29, 2018 at 22:17
  • 1
    Although your code is partly written in French the comments should be in English. If you want to communicate in french find a site where that is aloud. choice 1: you're not appending the output to the 'lettre' thus you get only the last one in the list_lettre. Commented Jan 29, 2018 at 22:17

3 Answers 3

1

Ok bear with me here, I only have the first part (the encoding), but I think it is enough for you to be able to do the rest:

code=list()
choix=int(input("Veuillez entrer 1 pour coder ou 2 pour decoder"))
if choix==1 :
    cle=int(input("Quelle est votre clé?"))
    phrase=input("Quelle est votre phrase? ")
    for el in phrase:
        if el.islower():
            r = ((ord(el) - ord('a')) + cle) % 26 + ord('a')
            code.append(chr(r))
        elif el.isupper():
            r = ((ord(el) - ord('A')) + cle) % 26 + ord('A')
            code.append(chr(r))
        else:
            code.append(el)

    print(code)

The tricky part here is this: ((ord(el) - ord('a')) + cle) % 26 + ord('a'), since you want to loop through the smaller case letters, you have to constrain your computation between ord('a') which is 97 and ord('z') which is 122. As @Malvolio suggested using the modulus, does the "restarting".

Sign up to request clarification or add additional context in comments.

3 Comments

@TemporalWolf Edited to remove any list comprehension.
Well apparently the theme of my evening was abusing. When it's not list comprehensions, it's ord(). Thanks for the feedback.
It's worth noting this only works for lowercase characters.
1

Here is a shift cipher function that might get you started.

def shift_cipher(phrase, shift):
    '''Ceaser Ciphers/Unciphers a string'''
    alphabet = list(string.ascii_lowercase)

    phrase = str(phrase)
    phrase = phrase.lower()
    new_phrase = ''
    for letter in phrase:
        shift_letter_index = alphabet.index(letter) + shift
        new_phrase += alphabet[shift_letter_index % 26]

    return new_phrase

2 Comments

Why not use the ascii_lowercase attribute from the string module to populate your alphabet rather than writing it by hand? Something like, alphabet = list(string.ascii_lowercase)?
Because I was copying from an old file and didn't think. That is a good suggestion.
0

Some points:

  • You didn't actually ask your question. What's the problem?
  • You make the liste_lettre but you never do anything with it.
  • The way to "restart" (usually called "wrap" in English) is to use the modulus. The modulus symbol in Python (and most programming languages) is %.

2 Comments

"enter" it? This is a coding issue. The expression chr(ord(c)+13) does not, as you've noticed, wrap properly, but chr(ord(a) + ((ord(c) + 13 - ord(a)) % 26)) will.
Pay it forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.