2
from string import ascii_lowercase as alphabet1
from string import ascii_uppercase as alphabet2

import letter as letter



def cipher(user_input, shift):
    cipher1 = {char: alphabet1[(i + shift) % 26] for i, char in enumerate(alphabet1)}
    cipher2 = {char: alphabet2[(i + shift) % 26] for i, char in enumerate(alphabet2)}
    
    caesar_cipher = ""
    
    for letter in user_input:
        caesar_cipher += cipher1.get(letter, letter)
    else:
        caesar_cipher += cipher2.get(letter, letter)
    return caesar_cipher


if __name__ == "__main__":
    
    user_input = input("Enter the String: ")
    
    shift = int(input("Enter shift: "))
    
    print("Caesar Cipher: " + cipher(user_input, shift))

I am performing Caeser cipher for both upper case and lower case characters. But the result is not correct. cipher1 is for lowercase and cipher 2 is for upper case. I have defined it in a function. And called it in main method the result obtained for lower case is:

Enter the String: abc
Enter shift: 2
Caesar Cipher: cdec

it should be cde

The result obtained for upper case is:

Enter the String: ABC
Enter shift: 2
Caesar Cipher: ABCE

It should be CDE

Jyoti
  • 31
  • 4
  • `...the result is not correct.`. How is it incorrect? What did you use for input, what was the output? What did you expect it to be? Do you suspect a particular part of the code? – wwii Dec 07 '22 at 04:19
  • [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – wwii Dec 07 '22 at 04:20
  • [collections.deque](https://docs.python.org/3/library/collections.html#collections.deque) has a rotate method which might be usefull. – wwii Dec 07 '22 at 04:24
  • i have updated the result, please check – Jyoti Dec 07 '22 at 04:28

2 Answers2

0

Check whether each letter is upper or lower case then use the corresponding cipher.

    ...
    
    for letter in user_input:
        cipher = cipher1 if letter in cipher1 else cipher2
        print(cipher[letter])

c
D
e

When the iterator is exhausted, the suite in the else clause, if present, is executed, and the loop terminates.

Your else clause was always executing.


For loop

wwii
  • 23,232
  • 7
  • 37
  • 77
0
from string import ascii_lowercase as alphabet1
from string import ascii_uppercase as alphabet2

import letter as letter



def cipher(user_input, shift):
    cipher1 = {char: alphabet1[(i + shift) % 26] for i, char in enumerate(alphabet1)}
    cipher2 = {char: alphabet2[(i + shift) % 26] for i, char in enumerate(alphabet2)}
    
    caesar_cipher = ""
    
    for letter in user_input:
        caesar_cipher += cipher1.get(letter, letter)
    else:
        caesar_cipher += cipher2.get(letter, letter)
    return caesar_cipher


if __name__ == "__main__":
    
    user_input = input("Enter the String: ")
    
    shift = int(input("Enter shift: "))
    
    print("Caesar Cipher: " + cipher(user_input, shift))
Aidis
  • 1,272
  • 4
  • 14
  • 31