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)
3 Answers 3
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".
3 Comments
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
ascii_lowercase attribute from the string module to populate your alphabet rather than writing it by hand? Something like, alphabet = list(string.ascii_lowercase)?Some points:
- You didn't actually ask your question. What's the problem?
- You make the
liste_lettrebut 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
chr(ord(c)+13) does not, as you've noticed, wrap properly, but chr(ord(a) + ((ord(c) + 13 - ord(a)) % 26)) will.
listcomme nom de variable. En effet, il s'agit d'un nom déjà utilisé comme nom de fonction et de type de données.print(lettre)instead oflistfor encoding. You're overriding the builtinlistwith your own list.list=list.append(lettre)setslisttoNone. It should just belist.append(lettre). See the dupe target.