















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Instructions for creating a Caesar Cipher project using Python. It covers the basics of Caesar Ciphers, encrypting and decrypting messages, setting up the Python file, and getting a secret letter. It also includes a bonus task to handle words instead of just letters.
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
This project was created by GPN Australia for GPN sites all around Australia!
Sydney, Canberra and Perth Girlsā Programming Network
Renee Noble Sarah Mac Alex McCulloch Jess Austin Bhavna Yadav Lyndsey Thomas Courtney Ross Maddie Jones Jess Austin Maggie Liuzzi
Sometimes we want to use different keys. So we want a different amount of rotation. Weāve given you a cipher wheel that lets you do this rotation! Grab your cipher wheel! You can rotate the inner wheel to try different shifts. Letās give it a try!
Letās try using a different key. Letās try 10. Rotate your cipher wheel so the green A lines up with the purple K. Encrypt this message:
Letās try a key of 24. Rotate your wheel 24 spots and encrypt this message. (You can ignore spaces)
Letās try a key of 7. Rotate your wheel 7 spots and decrypt this message:
Letās try a key of 11. Rotate your wheel 11 spots and decrypt this message:
CHECKPOINT If you can tick all of these off you can go to Part 1: ā You used your cipher wheel! ā You encrypted all the message!
ā You decrypted all the secret messages! Part 1: Setting up
Open the start menu, and type 'IDLEā. Select IDLE Python 3.
At the top of the file use a comment to write your name! Any line starting with # is a comment.
CHECKPOINT If you can tick all of these off you can go to Part 2: ā You should have a file called caesar_cipher.py ā Your file has your name at the top in a comment ā Run your file with F5 key and it does nothing!! Intro to Pytho n
CHECKPOINT If you can tick all of these off you can go to Part 3:
ā BONUS 2.6: Whatās the name of your secret language? Waiting for the next lecture? Try adding this bonus feature!!
secret message. ā BONUS 2.7: Customize welcome message Waiting for the next lecture? Try adding this bonus feature!! Letās customise the welcome message for the user.
message for Lyndsey.
Part 3: Getting a secret letter
Letās try doing this with pen and paper!
Counting the wheel can take a long time. You can use this table to look up the indexes of the 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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
So weāve got the position of the original letter, now we have to use the key to shift it to get a new index (later weāll use this to get the secret letter). To do that we need to work out the index of the secret letter which will be the current_index plus the key.
For example if we wanted to take the message ācipherā the table would look like this: Message current_letter current_index new_index cipher c 2? Since our key is 5 and our current_index is 2, we can work out that new_index should be the current_index plus the key, so itās 5+2 which is 7!
Next letās use the new index to get the secret letter from the alphabet.
Remember that you can get a letter out of a string like this: name = "Alex" index = 2 letter = name[index]
To modulo something by 5 you do this: number = 21 number = number % 5
Part 4: What about words?
Right now your code only gets the first letter of the message variable. The computer doesnāt know that there are more letters in message, we need to tell it!
Remember:
Now we want to add a for loop so that the computer looks at every letter in the message and turns it into a secret letter.
Hereās an example of a for loop. Donāt forget that when you code a for loop you need to use indents. for letter in word: print(letter) Loops
Use the print(variable, end='') that you learned in lecture slides to print your secret word on one line. CHECKPOINT If you can tick all of these off you can go to Part 4:
Part 5 : Dealing With Spaces
To fix this issue, we are going to add some if statements to check if the character is in the alphabet variable.
An else always needs to be attached to an if statement if raining == True: print ('oh no!') else: print ('Yay!') CHECKPOINT If you can tick all of these off you can go to Part 6: ā You have an if and else statement ā If your code can make more than one word secret ā Print out a secret sentence If Stateme nts
Part 6: Letās Get Cracking!
Before we can crack some codes, we need a message to decrypt. Use your program to get a secret message that we can decrypt (write down your key!!) Fill in the table below so that we can decrypt it later! Original Message Key Encrypted Message
At the moment your program can make a secret code, but if we gave that code to someone else, they wonāt be able to read it! We will need to add a mode where you can turn the secret message back into normal words using the same key that we used to make the secret message. We can call the modes āeā for encrypt and ādā for decrypt.
If the user chooses mode ādā we will need to change the key value to become the opposite of the encryption key value
7. Extension: Random Keys? So far we have had to choose a number to shift the letters by. Now, we want to let the computer choose what number to use by typing ārandomā when you ask how many letters to rotate by.
First we need to import random at the top of our program!
Now, when we ask the user for a key they can either type a number (the key) or the word ārandomā.
Donāt forget to use int() when you set the key to be the number
If the input is ā"random" the computer should pick a random number and store it in the key variable. Write code that chooses a random number between 1 and 25 and stores it in the key variable.
To get a random number between 1 and 10 you would write it like this: number = random.randrange(1, 11)
We need to know what the random number is so that we can give it to our friends to decode the message later. Add a print statement that prints the key before the secret message.
8. Extension: While loop What if we have heaps of messages to encrypt and decrypt? Letās loop our whole program so that we can use it for heaps of messages!
To make the code run over and over and over again so that we can keep encrypting all of our secret messages, we want to add a while loop near the top of the program.
Remember that you only want to put things you want to do EVERY TIME inside the while loop, so printing out the welcome statement and making your alphabet variable should be before the loop. Remember while loops look like this:
new line when it runs in a loop? Look at the text in red in the below box.