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))
添加回答
舉報