我在使用此功能时遇到了问题。 def encrypt(msg,code): '''(str,str) - >海峡 返回使用给定代码加密的msg。代码 是字母表加上空格的排序。'。 字符。代码中每个字符的位置 string给出替换字符的索引 常规字母表' abcdefghijklmnopqrstuvwxyz。'
In the first example below, 'h' is in position 20, so we
select the letter in position 20 in the alphabet, or 'u'.
>>> encrypt('hello there', '. zyxwvutsrqponmlkjihgfedcba')
'uxqqnbiuxkx'
>>> encrypt('hello there', '. zaybxcwdveuftgshriqjpkolnm')
'rlzzyborlsl'
'''
alpha = 'abcdefghijklmnopqrstuvwxyz'
i = 0
for char in len(msg[i]):
if msg[i] == code and msg[i] == alpha[i]:
return msg
我有,但它不起作用。有人可以帮帮我吗?
答案 0 :(得分:0)
+=
与以下内容相同:
coded_msg = coded_msg + alpha[code.index(char)]
答案 1 :(得分:-1)
这应该这样做:
def encrypt(msg, code):
coded_msg = ''
for char in msg:
coded_msg += alpha[code.index(char)]
return coded_msg
alpha = 'abcdefghijklmnopqrstuvwxyz. '
print( encrypt('hello there', '. zyxwvutsrqponmlkjihgfedcba') )