Hi everyone, I'm new to programming (and daniweb) and I'm having a small problem with a Caeser Cipher program I'm writing.

   messageE = input('\nPlease enter string to encrypt: ')
   offsetE = int(input('Please enter offset value (1 to 94): '))
   encryptedE = ''

   if offsetE < 1 or offsetE > 94:
       print('Incorrect input. Please enter a value between 1 and 94.')

       messageE = input('\nPlease enter string to encrypt: ')
       offsetE = int(input('Please enter offset value (1 to 94): '))

   for char in messageE:
       encryptedE = encryptedE + chr(ord(char) + offsetE)
       print('Encrypted string:\n', encryptedE)

Gives the output:

Please enter string to encrypt: hello
Please enter offset value (1 to 94): 1
Encrypted string:
 i
Encrypted string:
 if
Encrypted string:
 ifm
Encrypted string:
 ifmm
Encrypted string:
 ifmmp

I only want the final line "ifmmp" to show. How would I go about this? Thanks so much to anyone who can help.

Python's double-ended queue called deque in module collections has a method rotate() that is ideal for that sort of thing.

A special case is the rotational shift by 13 positions (half the alphabet) ...

''' str_rot13.py
rotate the letters in a string by 13 positions (half the alphabet)
tested with Python2.7.6 and Python3.2.5
'''

def str_rot13(text):
    '''
    cipher rotate text by 13 letters (half the alphabet)
    '''
    # ascii code of start and end letters
    a = ord('a')
    z = ord('z')
    A = ord('A')
    Z = ord('Z')
    cipher = ""
    for c in text:
        ascii = ord(c)
        # lower case
        if a <= ascii <= z:
            cipher += chr(a + (ascii - a + 13) % 26)
        # upper case
        elif A <= ord(c) <= Z:
            cipher += chr(A + (ascii - A + 13) % 26)
        # not a letter
        else:
            cipher += c
    return cipher

# test ...
text = "password!"

encoded = str_rot13(text)
print(text)
print(encoded)

decoded = str_rot13(encoded)
print(decoded)

''' result ...
password!
cnffjbeq!
password!
'''
This article has been dead for over six months. Start a new discussion instead.