Secret Code Coding

computer code電腦程式碼Diànnǎo chéngshì mǎ
secret秘密mìmì
code程式碼chéngshì mǎ
encode編碼biānmǎ
decode解碼jiěmǎ
message訊息xùnxí
spy間諜Jiàndié
mission使命Shǐmìng

You are a spy. You have a secret mission. You have a message:

The target is wkxq, the key is 14.

Who is the target? We have to save them. The target’s name is encoded.

We have a way to decode messages. First put the key in the middle of the circle. Look at what happens when we make the key higher or lower. Then to DECODE the message find the gray letter that matches our message. Then the blue or purple letter that is touching the gray letter is the decoded letter.

Let’s try encoding and decoding. First pick a number between 5 and 20.

Write this on your paper: key:_______

Write your name on your paper. To encode your name you need to find the blue and purple letters that match your name. Then the gray letters that touch the blue and purple letters are encoded.

Now fold your paper so that only you can only see the key and the encoded message.

Ok, now that we have tried to encode and decode, let’s try to write some code to do it for us.

We’re going to follow this lesson.

https://www.junyiacademy.org/article/7b3df2aa6ba345e3a0dbacbf56807476

This is a link to a python interpreter.

jumpto.cc/python-new

commands命令Mìnglìng
print列印Liè yìn
name = ‘Kyle’
print(‘this is my name: ‘,name)
this is my name: kyle
input輸入shūrù
name = input(‘please enter your name: ‘)
find尋找xúnzhǎo
name = ‘Kyle’
position = name.find(‘K’)
print(position)
0
% modulo% 模除Mó shù
integer | int()整數Zhěngshù
number = 14
print (int(number) +1
15
for loopfor 迴圈For huí quān
for each letter in the word do this對單字中的每個字母都執行此操作Duì dānzì zhōng de měi gè zìmǔ dōu zhíxíng cǐ cāozuò
word = ‘Kyle’
for letter in word:
print(letter)
K
y
l
e
variables變數biànshù
alphabet = abcdefghijklmnopqrstuvwxyz字母Zìmǔ
key鑰匙yàoshi
position位置wèizhì
message訊息xùnxí
letter字母Zìmǔ
new新的xīn de
camel case / camelCase駝峰式命名法Tuófēng shì mìngmíng fǎ
please enter an English letter:請輸入英文訊息:qǐng shūrù yīngwén xùnxí:
the new letter is:新信件是:Xīn xìnjiàn shì:
enter “e” for encode or “d” for decode:輸入“e”進行編碼或輸入“d”進行解碼:Shūrù “e” jìnxíng biānmǎ huò shūrù “d” jìnxíng jiěmǎ:
please input the message:請輸入留言內容:qǐng shūrù liúyán nèiróng:
please enter the key:請輸入密鑰:Qǐng shūrù mì yào:

% 模除(英語:modulo 有時也稱作 modulus),又稱模數取模取模運算等,它得出一個數除以另一個數的餘數。給定兩個正數:被除數 {\displaystyle a}除數 {\displaystyle n}{\displaystyle a\,\operatorname {modulo} \,n}(通常縮寫為 {\displaystyle a{\bmod {n}}}),得到的是使用歐幾里得除法時的餘數{\displaystyle a{\bmod {1}}} 所得之數被稱為 {\displaystyle a}小數部份。

舉個例子:計算表達式 {\displaystyle 5{\bmod {2}}} 得到 {\displaystyle 1},因為 {\displaystyle 5\div 2} 的商為 {\displaystyle 2} 而餘數為 {\displaystyle 1} ;而{\displaystyle 9{\bmod {3}}} 得到{\displaystyle 0},因為 {\displaystyle 9\div 3} 的商為 {\displaystyle 3} 而餘數為 {\displaystyle 0}

Example code:

doWhat = input(‘enter “e” for encode or “d” for decode’)

key = input(‘please enter the key:’) % 26 

if doWhat == “d” :

    key = 26 – int(key)

message = input(‘please input the message:’)

newMessage = ”

alphabet = ‘abcdefghijklmnopqrstuvwxyz’

for char in message:

  tempChar = char

  position = alphabet.find(char)

  #print (position)

  newPosition = (int(position) + int(key))%26

  newChar = alphabet[newPosition]

  newMessage += newChar

print(newMessage)