Night 21: Secret messages
คุณเรียนจบคืนนี้แล้วด้วย Python หรือรหัสเทียม และนับรวมในทั้งสองภาษา คุณสามารถฝึกอีกครั้งได้ตามต้องการ
Tonight: secret messages
- Your pet wants to send secret messages to its friends.
- Tonight's project: a secret code. You use loops over text and one new idea: character codes.
Words for tonight
| Word | Meaning |
|---|---|
| secret | something only some people may know |
| code | a number for each character; also a way to hide a message |
| shift | move along by some places |
Every character has a number
- The computer keeps each character as a number.
ASCgives the number.CHRgives the character back.- A single character can go in single quotes:
'a'is a CHAR.
OUTPUT ASC('a')
OUTPUT ASC('b')
OUTPUT CHR(99)
| Character | a |
b |
c |
… | z |
|---|---|---|---|---|---|
| Code | 97 | 98 | 99 | … | 122 |
Move a letter on
- Number, plus 1, back to a character:
DECLARE Letter : CHAR
DECLARE Code : INTEGER
Letter ← 'k'
Code ← ASC(Letter)
OUTPUT CHR(Code + 1)

Hide a whole word
- Do it for every letter in a loop:
DECLARE Word, Secret : STRING
DECLARE Index : INTEGER
Word ← "cat"
Secret ← ""
FOR Index ← 1 TO LENGTH(Word)
Secret ← Secret & CHR(ASC(MID(Word, Index, 1)) + 1)
NEXT Index
OUTPUT Secret
- To read the message, your friend moves each letter back by one:
- 1.
Week 3 is done!
- You can change text, build it, loop through it, and keep facts in records.
- Try a shift of 3 instead of 1. Send a secret message to a friend!
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Every character has a number code. Output the code of 'a' with ASC, then the character with code 98 with CHR. The output is 97, then b.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read one letter into a CHAR. Output the ถัดไป letter: a gives b, k gives l. (No z tonight.)
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read a word. Move every letter on by one, and output the secret word. cat gives dbu.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Make z go back to a. Read a small-letter word and move each letter on by one, so zoo gives app. Use MOD 26.
คลิก Run เพื่อดูผลลัพธ์ที่นี่