Friday 6 February 2015

February Python Challenge






1. Using the code below recreate the Caesar Cipher in Python
2. Add comments describing what each section does.
3. Test the cipher to ensure that it is working
4. Report your results in a word processed document with the following headings:


Introduction

-State user requirements for the Caesar cipher

Annotated Code

Output


N.B. You may use your own code if you wish but needs to have the same user requirements as the one below.  You may need to look at this code to work them out

DEADLINE MARCH 10th

def caesar_cipher(message, key, action):
    letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    number_in_list = len(letters)
    output_message = ""
    for next_character in message:
        in_list = False
        i = 0
        while i < number_in_list:
            new_position = 0
            if next_character == letters[i]:
                in_list = True
                # What happens next determined by value of action.
                if action == 'E':
                    new_position = i + key
                else:
                    new_position = i - key                   
                if new_position >= number_in_list:
                    new_position -= number_in_list
                output_char = letters[new_position]
            i += 1
        if in_list:
             output_message += output_char
        else:
            output_message += next_character
    return output_message

   
#Main program
print("CAESAR CIPHER")
print("")
print("OPTIONS")
print("")
flag = True
while flag:
    print("")
    print("1. Encrypt a message")
    print("2. Decrypt a message")
    print("3. Exit the program")
    answer = input("Please select an option. ")
    if answer == "1":
        message = input("Enter the message you want to encrypt: ")
        key = input("Enter the key: ")
        action = "E"
        print("The encoded message is: ")
        print(caesar_cipher(message, int(key), action))
    elif answer == "2":
        message = input("Enter the message you want to decrypt: ")
        key = input("Enter the key: ")
        action = "D"
        print("The decoded message is: "),
        print(caesar_cipher(message, int(key), action))
    elif answer =="3":
        flag = False

print("Good bye")


























29 comments:

  1. My own Caesar cipher code and report.

    https://docs.google.com/document/d/1-bar3eHuMTvcBdNcboWQQ8pRuX-oxmUtOy7Q3jYh5RM/edit?usp=sharing

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them.

      Delete
    2. I have done the necessary requirements on the same document with the same link.

      Delete
    3. I have identified all of the programming concepts in my code.

      Delete
  2. Caesar cipher report:
    https://docs.google.com/a/gillotts.org.uk/document/d/1wUQxhl-YUIpbAFdSM66oyRdjV7U_EE7yS81XD7cpTzM/edit?usp=sharing

    ReplyDelete
    Replies
    1. ease repeat with all the programming concepts identified in your anlysis. You have only picked some of them.

      Delete
    2. I have added improvements to the work on the same document.
      I have identified variable and explained the use of loops that are in the code.

      Delete
  3. My Caesar Cipher Report:
    https://docs.google.com/a/gillotts.org.uk/document/d/1Nx5q3Lsi8Y6GtFl85psgCWJrrw0Ox-Z0ibG9_Y-uthw/edit?usp=sharing

    ReplyDelete
    Replies
    1. Repeat with correct scientific labelling.

      Delete
    2. Thanks for your advice, I have taken this in to consideration and done the following:

      - Added more advanced terminology such as Variable, Function, Statement and loop.

      Here is the link to the edited version: https://docs.google.com/document/d/1Nx5q3Lsi8Y6GtFl85psgCWJrrw0Ox-Z0ibG9_Y-uthw/edit

      Delete
  4. My caeser cipher report
    https://docs.google.com/a/gillotts.org.uk/document/d/134CmJ5ndOBKpTCNRMbpHfcvOcbwoRC4q7wxrXHpjuoU/edit

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Delete
    2. I have labeled all of the code and use technical terminology

      https://docs.google.com/a/gillotts.org.uk/document/d/134CmJ5ndOBKpTCNRMbpHfcvOcbwoRC4q7wxrXHpjuoU/edit

      Delete
  5. My Caesar cipher report:
    https://docs.google.com/a/gillotts.org.uk/document/d/1MxqFL490HEO7yBeO_sTuvYzQ8q7bVMuw-r4scGenhCQ/edit?usp=sharing

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Delete
    2. I have now added more concepts to identify with in my anlaysis.

      Delete
  6. My caeser cipher report:
    https://docs.google.com/a/gillotts.org.uk/document/d/1Dcr16cTVBKlsxcKS9-7hkZ3bP2Ozc_71bmxujYUd8aY/edit

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Delete
    2. https://drive.google.com/drive/#folders/0B8Uizx1kjdLkM3BYSHNyTDdweUE/0B8Uizx1kjdLkaDNmUTlQY1lnd2M

      Delete
  7. https://docs.google.com/a/gillotts.org.uk/document/d/1iHAsg5zfrMQQT9AqLlKdYDWDpZFUH8ZHwW7UxCmASQY/edit?usp=sharing

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Variable Loop Conditional

      Delete
  8. Here is my caeser cipher document: https://docs.google.com/document/d/1xutY48rofuR8UO-zTPNGksxvZS-FBzFVwPQwax1TT_I/edit?usp=sharing

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Variable Loop Conditional

      Delete
    2. i have improved it by asdding more techniocal tyerms:
      https://drive.google.com/drive/#folders/0B3bktN3FhGEmM0JOY253dkJ4NTA/0B3bktN3FhGEmd2pVX3hkMmRaWEk

      Delete
  9. https://docs.google.com/a/gillotts.org.uk/document/d/1Dcr16cTVBKlsxcKS9-7hkZ3bP2Ozc_71bmxujYUd8aY/edit

    ReplyDelete
  10. https://docs.google.com/a/gillotts.org.uk/document/d/1dUTESdvuv_uGXSqREq6y-FjXc9s_HghxiaDBhE_dVDs/edit

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Delete
  11. https://docs.google.com/a/gillotts.org.uk/document/d/1a7ov8z52RoIi4_aTIWvTl6qRXdiwPQ0hUI_XAyZmq3Q/edit?usp=sharing

    ReplyDelete
    Replies
    1. Please repeat with all the programming concepts identified in your anlysis. You have only picked some of them

      Variable Loop Conditional

      Delete