1

I'm working on a Caesar Cipher for my class, and it prints everything just fine. However, if I put uppercase letters in the plaintext, the ciphered text comes out lowercase.

#Main program
def main():
    alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
    key = int(input("enter key: "))
    inputText = input("enter message: ")
    outputText = ''
    direction = input("Encrypt or decrypt (e/d)?:  ")

    for x in inputText:
        if x == ' ':
            outputText = outputText + ' '
        else:
            for i in range(0,len(alphabet),1):
                if x.lower() == alphabet[i]: 
                    if (direction == 'e'):
                        outputText = outputText + alphabet[i + key]
                        break    
                    else:
                        outputText = outputText + alphabet[i - key]
                        break
    print(outputText)


#Run and repeat loop        
while True:
    main()
    if input("Repeat the program? (Y/N): ").strip().upper() != 'Y': 
        break

Do I need to add an extra alphabet just for uppercase letters or is there a way for it to recognize them?

Anthony K
  • 11
  • 2
  • Rather than use `alphabet` variable, you could just translate each letter to ascii code and then add number to code to get result, here you can find more info https://stackoverflow.com/questions/227459/how-to-get-the-ascii-value-of-a-character e.g. "a" character is 97 code, then if you key is 3 you would get code "100" which stands for letter "d" then you could just subtract key once again and you have previous letter "a" and this works for capitalized as well – Patryk Brejdak May 02 '22 at 17:37
  • The code checks for `x.lower()`, and `alphabet` only contains lowercase letters, so of course you're only getting lowercase letters. – John Gordon May 02 '22 at 17:38

2 Answers2

0

When you add to outputText, you only ever add ' ', alphabet[i + key], or alphabet[i - key]. Your alphabet string contains only lowercase characters.

If you want your output text to correspond to the case of the current input letter (x), you'll need to manually alphabet[i +/- key].upper() when appropriate.

Josh Clark
  • 964
  • 1
  • 9
  • 17
0

Try this one

I added a comment where I change the code.

def main():
    alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
    key = int(input("enter key: "))
    inputText = input("enter message: ")
    outputText = ''
    direction = input("Encrypt or decrypt (e/d)?:  ")

    for x in inputText:
        if x == ' ':
            outputText = outputText + ' '
        else:
            for i in range(0,len(alphabet),1):
                t = x.isupper() # Check if the word is upper(True) or lower(False) 
                if x.lower() == alphabet[i]:
                    if (direction == 'e'):
                        if t: # if word is upper
                            outputText = outputText + alphabet[i + key].upper()
                        else: # if word is lower
                            outputText = outputText + alphabet[i + key]
                        break    
                    else:
                        if t: # if word is upper
                            outputText = outputText + alphabet[i - key].upper()
                        else: # if word is lower
                            outputText = outputText + alphabet[i - key]
                        break
    print(outputText)


#Run and repeat loop        
while True:
    main()
    if input("Repeat the program? (Y/N): ").strip().upper() != 'Y': 
        break
codester_09
  • 5,622
  • 2
  • 5
  • 27