-1

I am currently trying to write a program, for school, in order to encrypt and decrypt a inputted message. My code currently works but it is not fully complete. I am having trouble trying to allow it to enter an offset from values from 2-26 and I am having trouble making it repeat the question then carry on with the code until it gets the correct value. Right now it just says invalid message when a wrong number is entered but still carries on with the code. Also I need the encrypted or decrypted message to only be in the alphabet no other symbols or keys. Can anyone help please? This is my code currently.

result = ''

message = ''

choice = ''

offset=int(input("Enter an offset: "))

if offset >25 or offset <0:

    print("Invalid offset, please enter another offset: ")
else:

    print("Okay")


while choice != '3':


    choice = input("\nDo you want to encrypt or decrypt the message?\nEnter 1 to Encrypt, 2 to Decrypt, 3 to Exit Program: ")

    if choice == '1':
       message = input("\nEnter the message to encrypt: ")

    for i in range(0, len(message)):
       result = result + chr(ord(message[i]) - offset)

    print (result + '\n\n')
    result = ''

    elif choice == '2':
        message = input("\nEnter the message to decrypt: ")

        for i in range(0, len(message)):
            result = result + chr(ord(message[i]) + offset)

    print (result + '\n\n')
    result = ''

    elif choice != '3':
        print ("You have entered an invalid choice. Please try again.\n\n")
Generic Snake
  • 575
  • 1
  • 5
  • 13
  • The indentation *appears* to be incorrect. Please correct your post, otherwise it is difficult to follow. – cdarke Feb 14 '16 at 17:38
  • Hey, so I'm a little confused by how you want the program to loop, because I've ran it (fixed your indentation a little though first) and if an invalid number is entered it asks again like I'm guessing you're wanting to happen. – Generic Snake Feb 14 '16 at 17:45
  • @GenericSnake it will ask for it to be entered again but wont let you try and will carry on with the program instead of actually entering it again – Nadeem Rossan Feb 14 '16 at 17:46
  • Ah I see now, you mean at the offset, not the encrypt/decrypt stage – Generic Snake Feb 14 '16 at 17:51

1 Answers1

0

Ok, so I've not solved your swap in only letters problem, since I don't want to do all your schoolwork (hint: you're going to have to force your chr(number)to be inbetween alphabet values). But I have got everything looping for you.

def find_offset():
    offset = int(input("Enter an offset: "))
    if offset > 25 or offset < 0:
        print("Invalid offset, please enter another offset: ")
        find_offset()
    else:
        print("Okay")
        encrypt_fun(offset)


def encrypt_fun(offset):
    choice = ''
    while choice != '3':
        choice = input("\nDo you want to encrypt or decrypt the message?\nEnter 1 to Encrypt, 2 to Decrypt, 3 to Exit Program: ")
        if choice == '1':
            message = input("\nEnter the message to encrypt: ")

            for i in range(0, len(message)):
                result = chr(ord(message[i]) - offset)
                print(result, end=''),

        elif choice == '2':
            message = input("\nEnter the message to decrypt: ")

            for i in range(0, len(message)):
                result = chr(ord(message[i]) + offset)
                print(result, end=''),

        elif choice != '3':
            print("You have entered an invalid choice. Please try again.\n\n")

find_offset()

So what you have now is two functions instead of a singular script. Notice how the result, choice variables etc have been removed - they are now all declared inside the functions so you don't need them anymore! The first function finds your offset (and is called at the end, starting the program), if it's in the correct range it passes the value of the offset the encrypt_fun function, if it isn't then it runs find_offset again. I've tried to keep your while loop as much the same as it was - but it has been simplified a bit. Note how your declarations of result = '' have been removed. Well since it's a loop it gets reset anyway so it isn't necessary.

Hope this helps you out a bit. Good luck with your GCSEs. A link to help you with cryptography: https://inventwithpython.com/chapter14.html

Generic Snake
  • 575
  • 1
  • 5
  • 13