Demo entry 1487307

caesar cipher

   

Submitted by anonymous on Apr 22, 2015 at 09:20
Language: Python 3. Code size: 2.2 kB.

##By Ricardo F├║ster 10T

def checkIntKey():

    global key

    key = 1

    key = int(input("Please input the key: "))

    while key > 25:

            key = int(input("Please input the key: "))



def askStatus():

    print("Here are your options: ")

    print(

    """

    1: Decode

    2: Encode

    3: Abort

    """

    )



    global choice

    choice = int(input("please input your choice: "))



def encode():

     checkIntKey()

     global LETTERS

     LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "

     global lengthAlpha

     lengthAlpha = len(LETTERS)

     global message

     message = str(input("Please input the message you want to encrypt: "))

     message = message.upper()

     global encrypted

     encrypted = ""

     for letter in message:

         if letter in LETTERS:

             

             position = LETTERS.index(letter)#find the index of the letter in LETTERS

             position = position + key

             if position >= lengthAlpha:

                 position = position - lengthAlpha

             encrypted = encrypted + LETTERS[position]

     print(encrypted)



def decode():

    key2 = int(input("Please input the key: "))

    desencrypt = input("What message do you want to desencrypt : ")

    desencrypt = desencrypt.upper()

    global desencrypted

    desencrypted = ""

    for letter in desencrypt:

        if letter in LETTERS:

            

            position = LETTERS.index(letter)

            position = position + key2

            if position > lengthAlpha:

                position = position - lengthAlpha

            desencrypted = desencrypted + LETTERS[position]

    print(desencrypted)



print("Welcome to Caesar Cipher")

print()



askStatus()



while choice != 3:

    

    if choice == 1:

        decode()

        

        askStatus()

    elif choice == 2:

        

        encode()

        

        askStatus()

    else:

        print("Thank you for using Caesar Cipher")

        input("\nPress key to exit.")

    

    

          

          





    

        

    

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).