diff --git a/en/code/secret-messages-complete/main.py b/en/code/secret-messages-complete/main.py index 9b8f5630..8a223ef0 100644 --- a/en/code/secret-messages-complete/main.py +++ b/en/code/secret-messages-complete/main.py @@ -1,20 +1,18 @@ #!/bin/python3 alphabet = 'abcdefghijklmnopqrstuvwxyz' -newMessage = '' - -message = input('Please enter a message: ') - -key = input('Enter a key (1-26): ') +key = input('Please enter the key: ') key = int(key) +new_message = '' -for character in message: - if character in alphabet: - position = alphabet.find(character) - newPosition = (position + key) % 26 - newCharacter = alphabet[newPosition] - newMessage += newCharacter - else: - newMessage += character +message = input('Please enter a message: ') -print('Your new message is: ', newMessage) \ No newline at end of file +for character in message: + if character in alphabet: + position = alphabet.find(character) + new_position = (position + key) % 26 + new_character = alphabet[new_position] + new_message += new_character + else: + new_message += character +print(new_message) diff --git a/en/meta.yml b/en/meta.yml index 50c29e14..91fc37c0 100644 --- a/en/meta.yml +++ b/en/meta.yml @@ -17,15 +17,20 @@ software: python version: 3 last_tested: 2017-01-01 steps: -- title: 'Introduction: ' +- title: What you will make - title: The Caesar cipher -- title: 'Challenge: Use a Caesar cipher' -- title: Encrypting letters +- title: Use a Caesar cipher + challenge: true completion: - engaged -- title: 'Challenge: Variable keys' +- title: Encrypting letters + completion: + - internal +- title: Variable keys - title: Encrypting entire messages - title: Extra characters completion: - - internal -- title: 'Challenge: Encrypting and decrypting messages' + - external +- title: Encrypting and decrypting messages + challenge: true +- title: What can you do now? diff --git a/en/step_1.md b/en/step_1.md index 5bd1d22f..8dab8c1a 100644 --- a/en/step_1.md +++ b/en/step_1.md @@ -1,67 +1,7 @@ -## Introduction: +## What you will make -In this project, you'll learn how to make your own encryption program, to send and receive secret messages with a friend. This project ties in with the "Earth to Principia" activity on page 16 of the Space Diary. +In this project, you'll learn how to make your own encryption program, to send and receive secret messages with a friend. -### Additional information for club leaders - If you need to print this project, please use the [Printer friendly version](https://projects.raspberrypi.org/en/projects/secret-messages/print). - - ---- collapse --- ---- -title: Club leader notes ---- - - -## Introduction: -In this project, children will learn how to make an encryption program, to send and receive secret messages with a friend. This project introduces iteration (looping) over a text string. - -## Offline Resources -This project can be [completed offline](https://www.codeclubprojects.org/en-GB/resources/python-working-offline/) if preferred. - -You can find the completed project in the 'Volunteer Resources' section, which contains: - -+ messages-finished/messages.py -+ messages-finished/friends.py - -(All of the resources above are also downloadable as project and volunteer `.zip` files.) - -## Learning Objectives -+ Iteration (looping) over a string variable; -+ The `find()` method; -+ The modulus operator (`%`). - -## Challenges -+ Use a Caesar cipher - encrypt and decrypt letters and words manually; -+ Variable keys - allowing the user to input a chosen key; -+ Encrypting and decrypting messages - encrypting and decrypting whole messages; -+ Friendship calculator - applying text iteration to a new problem. - -## Frequently Asked Questions -+ When searching using `find()` or `if char in alphabet:`, note that searches are case-sensitive. Children can use: - - ```python - message = input("Please enter a message to encrypt: ").lower() - ``` - - to make the input lower case before searching. - ---- /collapse --- - - ---- collapse --- ---- -title: Project materials ---- -## Project resources -* [.zip file containing all project resources](resources/secret-messages-project-resources.zip) -* [Offline blank Python file](resources/new-new.py) - -## Club leader resources -* [.zip file containing all completed project resources](resources/secret-messages-volunteer-resources.zip) -* [secret-messages-finished/messages.py](resources/secret-messages-finished-messages.py) -* [offline complete 'Friendship calculator' challenge](resources/friendship-calculator-finished-friends.py) - ---- /collapse --- diff --git a/en/step_2.md b/en/step_2.md index 5e520449..a777ab69 100644 --- a/en/step_2.md +++ b/en/step_2.md @@ -6,21 +6,29 @@ You'll be using one of the oldest and most famous ciphers, the __Caesar cipher__ Before we start coding, let's try using the Caesar cipher to hide a word. +Hiding a word is called __encryption__. +Start by encrypting the letter 'a'. -+ Hiding a word is called __encryption__. +--- task --- - Let's start by encrypting the letter 'a'. To do this, we can draw the alphabet in a circle, like this: +Draw the alphabet in a circle, like this: - ![screenshot](images/messages-wheel.png) +![screenshot](images/messages-wheel.png) -+ To make a secret encrypted letter from a normal one, you need to have a secret key. Let's use the number 3 as the key (but you can use any number you like). +--- /task --- - To __encrypt__ the letter 'a', you just move 3 letters clockwise, which will give you the letter 'd': +To make a secret encrypted letter from a normal one, you need to have a secret key. - ![screenshot](images/messages-wheel-eg.png) +You could use the number 3 as the key (but you can use any number you like). -+ You can use what you've learnt to encrypt an entire word. For example, 'hello' encrypted is 'khoor'. Try it yourself. +To __encrypt__ the letter 'a', you just move 3 letters clockwise, which will give you the letter 'd': + +![screenshot](images/messages-wheel-eg.png) + +Use what you've learnt to encrypt an entire word. + +For example, 'hello' encrypted is 'khoor'. + h + 3 = __k__ + e + 3 = __h__ @@ -28,11 +36,10 @@ Before we start coding, let's try using the Caesar cipher to hide a word. + l + 3 = __o__ + o + 3 = __r__ -+ Getting text back to normal is called __decryption__. To decrypt a word, just subtract the key instead of adding it: +Getting text back to normal is called __decryption__. To decrypt a word, just subtract the key instead of adding it: + k - 3 = __h__ + h - 3 = __e__ + o - 3 = __l__ + o - 3 = __l__ + r - 3 = __o__ - diff --git a/en/step_3.md b/en/step_3.md index 1d104431..d7ad7943 100644 --- a/en/step_3.md +++ b/en/step_3.md @@ -1,8 +1,5 @@ ---- challenge --- -## Challenge: Use a Caesar cipher +## Use a Caesar cipher + Can you send a secret word to a friend? You'll both need to agree on a secret key before you start. You could even send entire sentences to each other! - - ---- /challenge --- \ No newline at end of file diff --git a/en/step_4.md b/en/step_4.md index 828f8930..729c9e56 100644 --- a/en/step_4.md +++ b/en/step_4.md @@ -1,6 +1,6 @@ ## Encrypting letters -Let's write a Python program to encrypt a single character. +Write a Python program to encrypt a single character. --- task --- @@ -10,7 +10,7 @@ Open the [Secret Messages starter project](https://editor.raspberrypi.org/en/pro --- task --- -Instead of drawing the alphabet in a circle, let's write it out as an `alphabet` variable. +Instead of drawing the alphabet in a circle, write it out as an `alphabet` variable. --- code --- --- @@ -29,7 +29,7 @@ Each letter of the alphabet has a position, starting at position 0. So the lette --- task --- -You can get a letter from your `alphabet` variable by writing the position in square brackets. +Output a letter from your `alphabet` variable by printing the letter at the position in square brackets. --- code --- --- @@ -160,7 +160,9 @@ print(new_position) --- task --- -+est out your new code. As your `key` is 3, it should add 3 to the `position` and store it in your `new_position` variable. +**Test:** Click the **Run** button. + +As your `key` is 3, it should add 3 to the `position` and store it in your `new_position` variable. For example, letter 'e' is at position 4. To encrypt, you add the `key` (3), giving 7. @@ -188,7 +190,7 @@ Notice how the `new_position` is 27, and there aren't 27 letters in the alphabet --- task --- -You can use a `%` to tell the new position to go back to position 0 once it gets to position 26. +Use a `%` to tell the new position to go back to position 0 once it gets to position 26. --- code --- --- @@ -214,7 +216,7 @@ print(new_position) --- task --- -Finally, you want to print the letter at the new position. +Finally, print the letter at the new position. For example, adding the key to the letter 'e' gives 7, and the letter at position 7 of the alphabet is 'h'. @@ -268,6 +270,3 @@ new_character = alphabet[new_position] print('The new character is: ', new_character) --- /code --- --- /task --- - - - diff --git a/en/step_5.md b/en/step_5.md index f20b2d6f..8afd9800 100644 --- a/en/step_5.md +++ b/en/step_5.md @@ -1,12 +1,29 @@ ---- challenge --- -## Challenge: Variable keys -Modify your program, so that the user can enter their own key to use. You'll need to get the user's input, and store it in the `key` variable. +## Variable keys -Remember to use the `int()` function to convert the input to a whole number. +Modify your program, so that the user can enter their own key to use. -You can then use a negative key to decrypt messages! +You'll need to get the user's input, and store it in the `key` variable. + +--- code --- +--- +language: python +filename: main.py +line_numbers: true +line_number_start: 1 +line_highlights: 2-3 +--- +alphabet = 'abcdefghijklmnopqrstuvwxyz' +key = input('Please enter the key: ') +key = int(key) +character = input('Please enter a character: ') +position = alphabet.find(character) +new_position = (position + key) % 26 ---- /challenge --- \ No newline at end of file +new_character = alphabet[new_position] +print('The new character is: ', new_character) +--- /code --- + +You can then use a negative key to decrypt messages! diff --git a/en/step_6.md b/en/step_6.md index 8d49fd8f..140c2d9d 100644 --- a/en/step_6.md +++ b/en/step_6.md @@ -4,33 +4,6 @@ Instead of just encrypting and decrypting messages one character at a time, let' --- task --- -Firstly, check that your code looks like this: - ---- code --- ---- -language: python -filename: main.py -line_numbers: true -line_number_start: 1 -line_highlights: ---- -alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 - -character = input('Please enter a character: ') - -position = alphabet.find(character) - -new_position = (position + key) % 26 - -new_character = alphabet[new_position] -print('The new character is: ', new_character) ---- /code --- - ---- /task --- - ---- task --- - Create a variable to store the new encrypted message. --- code --- @@ -39,10 +12,11 @@ language: python filename: main.py line_numbers: true line_number_start: 1 -line_highlights: 3 +line_highlights: 4 --- alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 +key = input('Please enter the key: ') +key = int(key) new_message = '' character = input('Please enter a character: ') @@ -67,10 +41,11 @@ language: python filename: main.py line_numbers: true line_number_start: 1 -line_highlights: 5 +line_highlights: 6 --- alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 +key = input('Please enter the key: ') +key = int(key) new_message = '' message = input('Please enter a message: ') @@ -95,10 +70,11 @@ language: python filename: main.py line_numbers: true line_number_start: 1 -line_highlights: 7 +line_highlights: 8 --- alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 +key = input('Please enter the key: ') +key = int(key) new_message = '' message = input('Please enter a message: ') @@ -139,10 +115,11 @@ language: python filename: main.py line_numbers: true line_number_start: 1 -line_highlights: 14-15 +line_highlights: 15-16 --- alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 +key = input('Please enter the key: ') +key = int(key) new_message = '' message = input('Please enter a message: ') @@ -170,10 +147,11 @@ language: python filename: main.py line_numbers: true line_number_start: 1 -line_highlights: 13, 15 +line_highlights: 14, 16 --- alphabet = 'abcdefghijklmnopqrstuvwxyz' -key = 3 +key = input('Please enter the key: ') +key = int(key) new_message = '' message = input('Please enter a message: ') diff --git a/en/step_7.md b/en/step_7.md index 5976e568..86986606 100644 --- a/en/step_7.md +++ b/en/step_7.md @@ -1,10 +1,10 @@ ## Extra characters -Some characters aren't in the alphabet, which causes an error. +Some characters are not in the alphabet, which causes an error. --- task --- -Test out your code with some characters that aren't in the alphabet. +Test out your code with some characters that are not in the alphabet. For example, you could use the message `hi there!!`. @@ -19,15 +19,15 @@ Notice that the space and the `!` characters are all encrypted as the letter 'c' --- task --- -To fix this, you only want to translate a character if it's in the alphabet. To do this, add an `if` statement to your code, and indent the rest of your code. +To fix this, only translate a character if it's in the alphabet. To do this, add an `if` statement to your code, and indent the rest of your code. --- code --- --- language: python filename: main.py line_numbers: true -line_number_start: 7 -line_highlights: 8 +line_number_start: 8 +line_highlights: 9 --- for character in message: if character in alphabet: @@ -45,20 +45,22 @@ print(new_message) --- task --- -Test your code with the same message. What happens this time? +**Test:** Click the **Run** button and test with the same message. + +What happens this time? ``` Please enter a message: hi there!! klwkhuh ``` -Now, your code just skips any character if it's not in the alphabet. +Your code just skips any character if it is not in the alphabet. --- /task --- ---- task --- +It would be better if your code just used the original character for anything not in the alphabet. -It would be better if your code didn't encrypt anything not in the alphabet, but just used the original character. +--- task --- Add an `else` statement to your code, which just adds the original character to the encrypted message. @@ -67,8 +69,8 @@ Add an `else` statement to your code, which just adds the original character to language: python filename: main.py line_numbers: true -line_number_start: 7 -line_highlights: 16-17 +line_number_start: 8 +line_highlights: 17-18 --- for character in message: if character in alphabet: @@ -87,7 +89,10 @@ print(new_message) --- /task --- --- task --- -Test your code. You should see that any character in the alphabet is encrypted, but any other characters are left alone! + +**Test:** Click the **Run** button. + +You should see that any character in the alphabet is encrypted, but any other characters are left alone! ``` Please enter a message: hi there!! diff --git a/en/step_8.md b/en/step_8.md index 7770ffd8..2b67b50c 100644 --- a/en/step_8.md +++ b/en/step_8.md @@ -1,7 +1,5 @@ ---- challenge --- -## Challenge: Encrypting and decrypting messages +## Encrypting and decrypting messages + Encrypt some messages, and give them to a friend along with the secret key. See if they can decrypt them using their program! You could also duplicate the project and create a separate program for decrypting messages. - ---- /challenge --- \ No newline at end of file diff --git a/en/step_9.md b/en/step_9.md new file mode 100644 index 00000000..e4f11cf3 --- /dev/null +++ b/en/step_9.md @@ -0,0 +1,13 @@ +## What can you do now? + +Try our [Password generator](https://projects.raspberrypi.org/en/projects/password-generator/){:target="_blank"} project where you will create a program to generate random passwords for you, so no one will be able to guess them! + +--- no-print --- + +Click **Run** to try it out! + + + +--- /no-print --- + +If you want to have more fun exploring Python, then you could try out any of [these projects](https://projects.raspberrypi.org/en/projects?software%5B%5D=python){:target="_blank"}. \ No newline at end of file