Skip to content

Commit

Permalink
Merge pull request #54 from raspberrypilearning/draft
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbRPF authored Dec 19, 2024
2 parents 7f2bf0c + 8884d04 commit 73dd399
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 164 deletions.
26 changes: 12 additions & 14 deletions en/code/secret-messages-complete/main.py
Original file line number Diff line number Diff line change
@@ -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)
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)
17 changes: 11 additions & 6 deletions en/meta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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?
64 changes: 2 additions & 62 deletions en/step_1.md
Original file line number Diff line number Diff line change
@@ -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.

<iframe src="https://editor.raspberrypi.org/en/embed/viewer/secret-messages-complete" width="600" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen> </iframe>

### 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 ---
25 changes: 16 additions & 9 deletions en/step_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,40 @@ 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__
+ l + 3 = __o__
+ 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__

7 changes: 2 additions & 5 deletions en/step_3.md
Original file line number Diff line number Diff line change
@@ -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 ---
17 changes: 8 additions & 9 deletions en/step_4.md
Original file line number Diff line number Diff line change
@@ -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 ---

Expand All @@ -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 ---
---
Expand All @@ -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 ---
---
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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 ---
---
Expand All @@ -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'.

Expand Down Expand Up @@ -268,6 +270,3 @@ new_character = alphabet[new_position]
print('The new character is: ', new_character)
--- /code ---
--- /task ---



29 changes: 23 additions & 6 deletions en/step_5.md
Original file line number Diff line number Diff line change
@@ -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 ---
new_character = alphabet[new_position]
print('The new character is: ', new_character)
--- /code ---

You can then use a negative key to decrypt messages!
52 changes: 15 additions & 37 deletions en/step_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---
Expand All @@ -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: ')
Expand All @@ -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: ')
Expand All @@ -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: ')
Expand Down Expand Up @@ -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: ')
Expand Down Expand Up @@ -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: ')
Expand Down
Loading

0 comments on commit 73dd399

Please sign in to comment.