在我的程序中,我试图制作一个简单的字母转换来加密输入消息的基本加密程序。用户提供输入,将字母与列表进行比较,并将整数输出返回到新列表中。新列表中的整数引用第一个列表的索引并返回相应的字符串(字母)。这将是我将来能够在简单程序中使用的功能。
所以我的主要问题是,我得到一个包含整数的列表,但我还没有想出一种方法来使用这些整数来引用另一个列表(alpha)的值来获取新字母。任何人都知道如何做到这一点,甚至有可能吗?谢谢!
下面是我的代码:
def alpha_trans():
# Asks user for input to be decrypted
word = input("Word or phrase to translate: ")
alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", " ", ".", "!", "?", "@", "#", "$", "%", "&"]
encrypt = []
# Takes input from user and compares to list above for decrypting
for letter in word:
# Takes index of characters from input, then adds 2
offset = alpha.index(letter) + 2
# Puts integers (alpha index + 2) into the encrypt list
encrypt.append(offset)
# Confirm contents of new list
print(encrypt)
# End of Function
alpha_trans()