我清理你的代碼:
plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
char_code = ord(char)
index_mod = index % 3
if index_mod == 0:
if char_code > 21:
offset = -21
else:
offset = 5
elif index_mod == 1:
if char_code > 24:
offset = -24
else:
offset = 2
else:
if char_code > 21:
offset = -20
else:
offset = 6
encrypted += chr(char_code + offset)
print encrypted
爲了好玩,它也可以做這樣的:
offsets = [{True: -21, False: 5}, {True: -24, False: 2}, {True: -20, False: 6}]
upper_limits = [21, 24, 21]
plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
char_code = ord(char)
index_mod = index % 3
offset = offsets[index_mod][char_code > upper_limits[index_mod]]
encrypted += chr(char_code + offset)
print encrypted
你甚至可以有
offsets = [[5, -21], [2, -24], [6, -20]]
但它不太清楚那裏發生了什麼事。
但是現在,我看到偏移的圖案(第二永遠是第一負26),應在代碼來完成:
offsets = [5, 2, 6]
upper_limits = [21, 24, 21]
plaintext = raw_input("Enter the string ")
encrypted = ''
for index, char in enumerate(plaintext):
char_code = ord(char)
index_mod = index % 3
offset = offsets[index_mod]
if char_code > upper_limits[index_mod]:
offset -= 26
encrypted += chr(char_code + offset)
print encrypted
您可以用空格混合選項卡。 –
它適合我。把你在這裏得到的東西複製粘貼回你的腳本中。 –
你可以完全刪除'c'並直接寫'f = ord(a [i])' – Daniel