Python Forum

Full Version: Coding caesar's cipher
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am starting off in python and as part of my learning I'm attempting to code a simple caeser's cipher.

I have figured out how to get this working if I don't have the get_offset as a function to call, but I would like to know how to do this as the ability to call functions is giving me headaches.

What I'm trying to achive:

1: program a function (get_offset) to input the shift of the caeser ciper, to be called once entering the string to encrypt/decrypt as per menu options 1 and 2.
2: have this function loop back to the shift int(input) whenever a number that isn't a digit is entered, or a digit is entered that is not between 1 and 94. I currently only have if statements there and those are not working also
shift = int(input('Please enter offset value (1 to 94): '))
3: additional question, I'm struggling to understand if in this case I should have my function as get_offset(shift): or get_offset(): and why either should be the case.

TIA

def menuChoice():

    print ('')
    print ('Menu')
    print ('')
    print ('1. Encrypt string')
    print ('2. Decrypt string')
    print ('3. Brute force decryption')
    print ('4. Quit')
    print ('')

    choice = input('What would you like to do (1,2,3,4)? ')

    while choice != '1' and choice != '2' and choice != '3' and choice != '4':
        print ('Invalid choice, please enter either 1, 2, 3 or 4.')
        choice = input('\nWhat would you like to do (1,2,3,4)? ')
    
    return choice

def get_offset():

    shift = int(input('Please enter offset value (1 to 94): '))
    
    if shift.isdigit == 0:
        shift = int(input('Please enter offset value (1 to 94): '))
    elif shift <0 or shift > 95:
        shift = int(input('Please enter offset value (1 to 94): '))
    else:
        return shift
    
choice = menuChoice()

if choice == '1':
    encrypt = input('Please enter string to encrypt: ')
    shift = get_offset()
    cipher = ''

    for i in encrypt:
        cipher += chr((ord(i) - 32 + shift) % 95 + 32)
            
    print ('\nEncrypted string:')
    print (cipher)

elif choice == '2':
    decrypt = input('Please enter string to decrypt: ')
    shift = get_offset()
    cipher = ''

    for i in decrypt:
        cipher += chr((ord(i) - 32 - shift) % 95 + 32)
            
    print ('\nDecrypted string:')
    print (cipher)

elif choice == '3':
    decrypt = input('Please enter string to decrypt: ')
    brute_shift = 1
    cipher = ''

    for index in range(94):
        for i in decrypt:
            cipher += chr((ord(i) - 32 - brute_shift) % 95 + 32)
        print ('Offset: ', brute_shift,'= Decrytped string: ', cipher)
        brute_shift += 1
        cipher = ''
Output:
Menu 1. Encrypt string 2. Decrypt string 3. Brute force decryption 4. Quit What would you like to do (1,2,3,4)? 1 Please enter string to encrypt: one more time Please enter offset value (1 to 94): 3[output]
Error:
Traceback (most recent call last): File "C:/Users/Angelica/Documents/caesercipher.py", line 44, in <module> shift = get_offset() File "C:/Users/Angelica/Documents/caesercipher.py", line 31, in get_offset if shift.isdigit == 0: AttributeError: 'int' object has no attribute 'isdigit'
Not .isdigit, use .isdigit().
Also, I do not think the way you use .isdigit() is correct. See this
Thanks. My code was all completely wrong and I got it figured out, but the missing () was a good spot

My function became:

def get_offset():
    while True:
        shift = input('Please enter offset value (1 to 94): ')
        if shift.isdigit():
            shift = int(shift)
            if shift >=1 and shift <= 94:
                return shift
and to call the function later became
shift = get_offset()
Maybe you also want to consider a small improvement:

choices = (1,2,3,4)
choice = 1
if choice in choices:
    print("success")
Paul