@@ -55,27 +55,27 @@ SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
55
55
translated = ' '
56
56
57
57
for symbol in message:
58
- 22 . # Note: Only symbols in the SYMBOLS string can be
58
+ # Note: Only symbols in the SYMBOLS string can be
59
59
encrypted/ decrypted.
60
- 23 . if symbol in SYMBOLS :
61
- 24 . symbolIndex = SYMBOLS .find(symbol)
62
-
63
- 26 . # Perform encryption/decryption:
64
- 27 . if mode == ' encrypt' :
65
- 28 . translatedIndex = symbolIndex + key
66
- 29 . elif mode == ' decrypt' :
67
- 30 . translatedIndex = symbolIndex - key
68
-
69
- 32 . # Handle wraparound, if needed:
70
- 33 . if translatedIndex >= len (SYMBOLS ):
71
- 34 . translatedIndex = translatedIndex - len (SYMBOLS )
72
- 35 . elif translatedIndex < 0 :
73
- 36 . translatedIndex = translatedIndex + len (SYMBOLS )
74
-
75
- 38 . translated = translated + SYMBOLS [translatedIndex]
76
- 39 . else :
77
- 40 . # Append the symbol without encrypting/decrypting:
78
- 41 . translated = translated + symbol
60
+ if symbol in SYMBOLS :
61
+ symbolIndex = SYMBOLS .find(symbol)
62
+
63
+ # Perform encryption/decryption:
64
+ if mode == ' encrypt' :
65
+ translatedIndex = symbolIndex + key
66
+ elif mode == ' decrypt' :
67
+ translatedIndex = symbolIndex - key
68
+
69
+ # Handle wraparound, if needed:
70
+ if translatedIndex >= len (SYMBOLS ):
71
+ translatedIndex = translatedIndex - len (SYMBOLS )
72
+ elif translatedIndex < 0 :
73
+ translatedIndex = translatedIndex + len (SYMBOLS )
74
+
75
+ translated = translated + SYMBOLS [translatedIndex]
76
+ else :
77
+ # Append the symbol without encrypting/decrypting:
78
+ translated = translated + symbol
79
79
80
80
# Output the translated string:
81
81
print (translated)
@@ -237,7 +237,7 @@ The letter is y
237
237
凯撒密码中的第 23 行有另一种 Python 指令——` if ` 语句:
238
238
239
239
``` py
240
- 23 . if symbol in SYMBOLS :
240
+ if symbol in SYMBOLS :
241
241
```
242
242
243
243
你可以把一个` if ` 语句理解为,“如果这个条件是` True ` ,执行下面块中的代码。否则,如果是` False ` ,跳过这个代码块。”一个` if ` 语句的格式是使用关键字` if ` 后跟一个条件,再跟一个冒号(` : ` )。与循环一样,要执行的代码缩进在一个块中。
@@ -307,7 +307,7 @@ The letter is y
307
307
` caesarCipher.py ` 中的第 23 行也使用了` in ` 操作符:
308
308
309
309
``` py
310
- 23 . if symbol in SYMBOLS :
310
+ if symbol in SYMBOLS :
311
311
```
312
312
313
313
一个` in ` 操作符可以连接两个字符串,如果第一个字符串在第二个字符串内,它将计算为` True ` ,否则计算为` False ` 。` in ` 操作符也可以与` not ` 配对,后者的作用正好相反。在交互式 shell 中输入以下内容:
@@ -336,7 +336,7 @@ The letter is y
336
336
第 24 行找到了` SYMBOLS ` 字符串中的索引,其中` symbol ` 是:
337
337
338
338
``` py
339
- 24 . symbolIndex = SYMBOLS .find(symbol)
339
+ symbolIndex = SYMBOLS .find(symbol)
340
340
```
341
341
342
342
这段代码包含一个方法调用。* 方法* 就像函数一样,只不过它们附加了一个带句点的值(或者在第 24 行,一个包含值的变量)。这个方法的名字是` find() ` ,它被存储在` SYMBOLS ` 字符串值中用于调用。
@@ -386,8 +386,8 @@ The letter is y
386
386
密码程序只能加密或解密符号集中的符号:
387
387
388
388
``` py
389
- 23 . if symbol in SYMBOLS :
390
- 24 . symbolIndex = SYMBOLS .find(symbol)
389
+ if symbol in SYMBOLS :
390
+ symbolIndex = SYMBOLS .find(symbol)
391
391
```
392
392
393
393
所以在运行第 24 行的代码之前,程序必须弄清楚` symbol ` 是否在符号集中。然后可以在` SYMBOLS ` 中找到` symbol ` 所在的索引。` find() ` 调用返回的索引存储在` symbolIndex ` 中。
@@ -397,11 +397,11 @@ The letter is y
397
397
caesarCipher.py
398
398
399
399
``` py
400
- 26 . # Perform encryption/decryption:
401
- 27 . if mode == ' encrypt' :
402
- 28 . translatedIndex = symbolIndex + key
403
- 29 . elif mode == ' decrypt' :
404
- 30 . translatedIndex = symbolIndex - key
400
+ # Perform encryption/decryption:
401
+ if mode == ' encrypt' :
402
+ translatedIndex = symbolIndex + key
403
+ elif mode == ' decrypt' :
404
+ translatedIndex = symbolIndex - key
405
405
```
406
406
407
407
` mode ` 变量包含一个字符串,告诉程序应该加密还是解密。如果这个字符串是` 'encrypt' ` ,那么第 27 行的` if ` 语句的条件将是` True ` ,执行第 28 行将` key ` 加上` symbolIndex ` (跳过` elif ` 语句后的块)。否则,如果` mode ` 是` 'decrypt' ` ,则执行第 30 行减去` key ` 。
@@ -411,11 +411,11 @@ caesarCipher.py
411
411
当我们在第一章中用纸和笔实现凯撒密码时,有时增加或减少密钥会导致一个大于或等于符号集大小或小于零的数。在这些情况下,我们必须增加或减少符号集的长度,以便它能够“回绕”,或者返回到符号集的开头或结尾。我们可以使用代码` len(SYMBOLS) ` 来做这件事,它返回` 66 ` ,即` SYMBOLS ` 字符串的长度。第 33 到 36 行在密码程序中处理这种回绕。
412
412
413
413
``` py
414
- 32 . # Handle wraparound, if needed:
415
- 33 . if translatedIndex >= len (SYMBOLS ):
416
- 34 . translatedIndex = translatedIndex - len (SYMBOLS )
417
- 35 . elif translatedIndex < 0 :
418
- 36 . translatedIndex = translatedIndex + len (SYMBOLS )
414
+ # Handle wraparound, if needed:
415
+ if translatedIndex >= len (SYMBOLS ):
416
+ translatedIndex = translatedIndex - len (SYMBOLS )
417
+ elif translatedIndex < 0 :
418
+ translatedIndex = translatedIndex + len (SYMBOLS )
419
419
```
420
420
421
421
如果` translatedIndex ` 大于等于` 66 ` ,则第 33 行的条件为` True ` ,执行第 34 行(跳过第 35 行的` elif ` 语句)。从` translatedIndex ` 中减去` SYMBOLS ` 的长度将变量的索引指向` SYMBOLS ` 字符串的开头。否则 Python 会检查` translatedIndex ` 是否小于` 0 ` 。如果条件是` True ` ,则执行第 36 行,并且` translatedIndex ` 绕到` SYMBOLS ` 字符串的末尾。
@@ -425,7 +425,7 @@ caesarCipher.py
425
425
现在您已经在` translatedIndex ` 中有了translated变量中符号集的索引,` SYMBOLS[translatedIndex] ` 将对translated变量中符号集求值。第 38 行使用字符串连接将这个加密/解密的符号添加到` translated ` 字符串的末尾:
426
426
427
427
``` py
428
- 38 . translated = translated + SYMBOLS [translatedIndex]
428
+ translated = translated + SYMBOLS [translatedIndex]
429
429
```
430
430
431
431
最终,` translated ` 字符串将是整个编码或解码的消息。
@@ -435,9 +435,9 @@ caesarCipher.py
435
435
` message ` 字符串可能包含不在` SYMBOLS ` 字符串中的字符。这些字符在密码程序的符号集之外,无法加密或解密。相反,它们将被直接追加到` translated ` 字符串中,这发生在第 39 到 41 行:
436
436
437
437
``` py
438
- 39 . else :
439
- 40 . # Append the symbol without encrypting/decrypting:
440
- 41 . translated = translated + symbol
438
+ else :
439
+ # Append the symbol without encrypting/decrypting:
440
+ translated = translated + symbol
441
441
```
442
442
443
443
第 39 行的` else ` 语句有四个缩进空间。如果您查看上面行的缩进,您会看到它与第 23 行的` if ` 语句成对出现。尽管在这个` if ` 和` else ` 语句之间有很多代码,但它们都属于同一个代码块。
0 commit comments