Python 3.x Python-遍历列表,通过索引交换alphbets?
我需要写一个代码,使字母旋转,通过2个列表 所以我需要定义一个函数,比如说它叫做rotate_text 传递了2个参数,1为字符串,1为整数 这是我目前的代码:Python 3.x Python-遍历列表,通过索引交换alphbets?,python-3.x,Python 3.x,我需要写一个代码,使字母旋转,通过2个列表 所以我需要定义一个函数,比如说它叫做rotate_text 传递了2个参数,1为字符串,1为整数 这是我目前的代码: def rotate_text(text, n): plaintext = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'] ciphertext = ['FGHIJKLMNOPQRSTUVWYXZABCDE'] rotated_text = [] for i in plaintext: rotated_text =
def rotate_text(text, n):
plaintext = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
ciphertext = ['FGHIJKLMNOPQRSTUVWYXZABCDE']
rotated_text = []
for i in plaintext:
rotated_text = ciphertext[plaintext[i + n]]
result = ''.join(rotated_text)
return result
所以它需要做的是,如果我把ABC作为参数文本,把2作为参数n
A应返回CDE作为结果。或者狗和11应该返回OBK。我真的不认为我需要那个密文列表,所以我想我会把它去掉,但我如何让这个代码工作呢
如果程序得到ABC作为文本,它应该从明文列表中找到A的索引,并从该索引中找到+n,然后从明文列表中找到字母满足加上n的索引,然后。。。。我头痛
有人能帮忙吗?这段代码怎么样?
文本
应仅为大写字符
def rotate_text(text, n):
for i in len(text):
number = ord(text[i]) - ord('A')
number = (number + n) % 26
text[i] = chr(number + ord('A'))
return text
如果您也要使用小写,则应使用
If
语句。它在行号=(number+n)%26处出错。它说的语法无效。