【发布时间】:2021-06-09 19:57:50
【问题描述】:
问。编写一个名为shift_string 的函数,它接受一个字符串和一个整数n 作为参数,并返回一个新字符串,其中字符串的每个字母都由n 字母移动。它也应该适用于以相反顺序排列的否定字母。
到目前为止,我想出了这个:
usr_input=input('Please enter string: ')
n=input('enter shifts: ')
encryption= ""
def shift_string(usr_input,n):
for i in usr_input:
if i.isupper():
i_unicode=ord(i) #find position
i_index=i_unicode-ord('A')
new_index= (i_index + n)
new_unicode= new_index +ord('A') #to perform shift
new_character=chr(new_unicode)
encryption= encryption+new_character #to append string
else:
encryption=encryption+i #for non-uppercase
print('Encrypted text is',encryption)
At encryption= encryption+new_character我收到错误消息:
“在赋值前引用的第 23 行封闭范围内定义的局部变量‘加密’...(pyflakes E)”
【问题讨论】:
-
请从intro tour 重复on topic 和how to ask。每个帖子都会收到一个问题。
-
你的最后一段在这里完全超出了范围。 “我被卡住了”不是 Stack Overflow 问题。 "Can Someone Help Me?" is not a valid SO question。这表明对于 Stack Overflow 而言,一系列需求过于广泛。
-
您的代码不会产生第二个错误;你永远不会调用函数。
标签: python string encryption