2 回答
TA贡献2019条经验 获得超9个赞
您可以将alphabet[new_char]追加到列表中,然后使用 join 将其打印为字符串。下面的示例代码(经过编辑以让非字母数字的字符保留在原处):
import string
alphabet = string.ascii_lowercase
message = "the quick brow???nxa2 fox jumps over the lazy dog"
key = 7
lst=[]
for char in message:
if char.isalpha() is True:
new_char = key + (alphabet.index(char))
if new_char > 25:
new_char = new_char % 26
lst.append(alphabet[new_char])
else:
lst.append(char)
print(''.join(i for i in lst))
TA贡献1811条经验 获得超4个赞
"""Cypher program."""
import string
alphabet = string.ascii_lowercase
message = "thequick0brownfox jumpsoverthelazydog"
def transform(char,key):
if char.isalpha():
new_char = key + (alphabet.index(char))
if new_char > 25:
new_char = new_char % 26
return alphabet[new_char]
return char
key = 7
# faster string comprehension
decripted = [transform(char,key) for char in message]
print(decripted)
# or
# "".join - puts all elements of an array toghether in a string using a separator
print("".join(decripted))
添加回答
举报