Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding caesar's cipher
#1
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'
Reply
#2
Not .isdigit, use .isdigit().
Also, I do not think the way you use .isdigit() is correct. See this
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
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()
Reply
#4
Maybe you also want to consider a small improvement:

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cesar Cipher ForsakenDusk 5 413 Apr-07-2024, 04:30 PM
Last Post: Pedroski55
Question Rsa Cipher Paragoon2 3 616 Nov-27-2023, 12:30 PM
Last Post: snippsat
  RSA Cipher with blocks Paragoon2 0 481 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  Caesar Cipher Help pbrowne 2 2,151 Jun-30-2021, 02:36 PM
Last Post: deanhystad
  Learning Python with a Caesar cipher Drone4four 5 4,782 Nov-21-2020, 07:21 PM
Last Post: bowlofred
  Caesar Cypher--- I don't understand why it doesn't work ironsheep 12 5,862 Nov-03-2018, 06:53 PM
Last Post: j.crater
  Newbie: Help with code related to Caesar Cipher jessiblah 2 3,386 May-15-2018, 04:28 PM
Last Post: nilamo
  Problem with caesar cipher lucaron 2 2,934 Feb-05-2018, 05:17 PM
Last Post: lucaron
  Decrypt Caesar Cipher WantToImprove 22 17,483 Jan-30-2018, 08:43 AM
Last Post: WantToImprove
  Simple Caesar Cipher Help as1221 6 5,345 Jun-15-2017, 10:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020