From 7b83353dd9a9373ee4332fe7ed4cca8dc9d427cb Mon Sep 17 00:00:00 2001 From: pythonboi Date: Wed, 15 Sep 2021 11:43:47 -0400 Subject: [PATCH 1/2] fix the indentation for decrypt loop --- Caesar_Cipher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Caesar_Cipher.py b/Caesar_Cipher.py index f5f79d1..ec7fba3 100644 --- a/Caesar_Cipher.py +++ b/Caesar_Cipher.py @@ -69,7 +69,7 @@ def encrypt(text, shift): def decrypt(text, shift): deNew = '' - for check in text + for check in text: getDecry = alphabet.index(check) - shift deNew += alphabet[getDecry] From 2a4bf0288caa1e6020ae0b9f223f48bf7c0f1b37 Mon Sep 17 00:00:00 2001 From: pythonboi Date: Thu, 16 Sep 2021 01:00:14 -0400 Subject: [PATCH 2/2] update the code to one function with three argument --- Caesar_Cipher.py | 74 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/Caesar_Cipher.py b/Caesar_Cipher.py index ec7fba3..9e1bdc5 100644 --- a/Caesar_Cipher.py +++ b/Caesar_Cipher.py @@ -5,6 +5,7 @@ text = input("Type your message:\n").lower() shift = int(input("Type the shift number:\n")) + # # # def encrypt(text, shift): @@ -50,33 +51,56 @@ # BETTER COPY/Solution +# +# def encrypt(text, shift): +# newEn = '' +# for count in text: +# en = alphabet.index(count) + shift +# getLen = len(alphabet) +# if en >= getLen: +# total = en - getLen +# fw = alphabet[total] +# newEn += fw +# +# else: +# newEn += alphabet[en] +# +# print(f"The encode text is {newEn}") # newEn) +# +# +# def decrypt(text, shift): +# deNew = '' +# for check in text: +# getDecry = alphabet.index(check) - shift +# deNew += alphabet[getDecry] +# +# print(f"The decoded text is {deNew}") +# +# +# if direction == "encode": +# encrypt(text=text, shift=shift) +# elif direction == "decode": +# decrypt(text=text, shift=shift) -def encrypt(text, shift): - newEn = '' - for count in text: - en = alphabet.index(count) + shift - getLen = len(alphabet) - if en >= getLen: - total = en - getLen - fw = alphabet[total] - newEn += fw - - else: - newEn += alphabet[en] - - print(f"The encode text is {newEn}") # newEn) - +# Combining all the function and statement to one function call -def decrypt(text, shift): - deNew = '' - for check in text: - getDecry = alphabet.index(check) - shift - deNew += alphabet[getDecry] +def caesar(text, shift, direction): + codeChar = '' + if direction == 'encode': + for me in text: + ch = alphabet.index(me) + shift + if ch > len(alphabet): + alpLeft = ch - len(alphabet) + # ch - len(alphabet) + codeChar += alphabet[alpLeft] + print(f'The encode code is {codeChar}') - print(f"The decoded text is {deNew}") + elif direction == 'decode': + decChar = '' + for new in text: + deco = alphabet.index(new) - shift + decChar += alphabet[deco] + print(f'The decode code is {decChar}') -if direction == "encode": - encrypt(text=text, shift=shift) -elif direction == "decode": - decrypt(text=text, shift=shift) +caesar(text=text, shift=shift, direction=direction)