Home > Software design >  Caesar cipher not functioning, not sure what's wrong with it
Caesar cipher not functioning, not sure what's wrong with it

Time:04-04

I need to create a simple Caesar cipher encoder/decoder and im not sure the best way to do it, strings? lists? loops?

Word = input("What do you want to decode")
Shift = input("What do you want the shift to be?") 
alphabet = ["A","B", "C","D","E","F","G","H","I","J","K","L","M","N",
            "O","P","Q","R","S","T","U","V","W","X","Y","Z"]

for letters in Word:
    if letters == [alphabet.index(letters)]:
        print [alphabet.index(letters   Shift)]       

for a in alphabet:
    if a == letters:
        print (letters (alphabet.index(letters)))

As you can tell I tried for loops to separate list items and compare but it didn't work so I'm not sure how to proceed, cannot use the translate or order function.

CodePudding user response:

  • Your alphabet list is missing at least one letter, so that might mess with your results. I suggest using string.ascii_uppercase instead.

  • In the statement if letters == [alphabet.index(letters)] you're mixing up the letter (letters -- this actually is just one letter!) with the index of the letter in alphabet; it's impossible for them to ever be the same since one is a string and another is an int. You don't need to loop to find the index anyway; the index function does that for you.

  • Make sure to "wrap" the shift around the end of the alphabet! The mod (%) operator is an easy way to do that.

  • Careful of characters that aren't in alphabet at all.

import string

word = input("What do you want to decode? ").upper()
shift = int(input("What do you want the shift to be? "))
alphabet = string.ascii_uppercase

print(''.join(
    alphabet[
        (alphabet.index(c)   shift) % len(alphabet)
    ] if c in alphabet else c for c in word
))
What do you want to decode? ZEBRA STRIPES
What do you want the shift to be? 12
LQNDM EFDUBQE

CodePudding user response:

use this loop

import string
Word = input("What do you want to decode")
Shift = input("What do you want the shift to be?") 
alphabet = string.ascii_uppercase
ouput = ""
for i in Word:
    if i.upper() in alphabet:
        ouput  = alphabet[alphabet.find(i.upper()) int(Shift)]
    else:
        ouput =i 
print(ouput)

CodePudding user response:

word = input("What do you want to decode ")
shift = input("What do you want the shift to be? ")

alphabet = ["A","B", "C","D","E","F","G","H","I","J","K","L","M","N",
            "O","P","Q","R","S","T","U","V","W","X","Y","Z"]

def caesar_encrypt(word: str, shift: int):
    return ''.join(alphabet[(alphabet.index(char)   shift) % len(alphabet)]
                   if char in alphabet else char for char in word)

def caesar_decript(word: str, shift: int):
    return caesar_encrypt(word, 26-shift)

word = word.upper()
shift = int(shift)

encrypted_word = caesar_encrypt(word, shift)
print(encrypted_word)

decripted_word = caesar_decript(encrypted_word, shift)
print(decripted_word)
  • Related