JPtheK9
JPtheK9

Reputation: 307

How do I make my Caeser Cipher work?

This is what I have so far for my Caeser Cipher program.

 import string
    character = []
    message = raw_input('What is your message? ').lower()
    shift = raw_input('What is your shift key? ')
    code = raw_input('Would you like to cipher(c) or decipher(d)? ')
    if str(code) == 'd':
        for character in message:
            number = ord(character) - int(shift)
            if number <= 96:
                number = ord(character) + 26 - int(shift)
            character = chr(number)
    elif str(code) == 'c':
        for character in message:
            number = ord(character) + int (shift)
            if number >= 123:
                number = ord(character) - 26 + int(shift)
            character = chr(number)
    print(str(character))

Every time I use this program, I get back the encrypted or decrypted message of only the last letter of the line I type for the message. I'm not sure how to print my entire encrypted or decrypted message out.

Upvotes: 0

Views: 159

Answers (1)

onemouth
onemouth

Reputation: 2287

The problem is that you only print once outside the for loop.

You can move the print statement inside the for loop.

if str(code) == 'd':
    for character in message:
        number = ord(character) - int(shift)
        if number <= 96: 
            number = ord(character) + 26 - int(shift)
        character = chr(number)
        print( str(character))
elif str(code) == 'c':
    for character in message:
        number = ord(character) + int (shift)
        if number >= 123:
            number = ord(character) - 26 + int(shift)
        character = chr(number)
        print(str(character))

Upvotes: 2

Related Questions