我不断收到一个错误:NameError: name 'text' is not defined
def Encryption(i, j):
diction = {'a':'x1', 'b':'3', 'c':'lk', 'd':'$%', 'e':'^%', 'f':'(*', 'g':'-+', 'h':'il', 'i':'z@', 'j':'@#', 'k':'}{', 'l':'-*', 'm':'p', 'n':':l', 'o':'!#', 'p':'1%', 'q':'k<', 'r':'/', 's':'>', 't':'@', 'u':'if', 'v':'q#$', 'w':'^#1', 'x':'5-=', 'y':'n?', 'z':'v'}
my_text = input("What would you like to encrypt?")
text = my_text.lower()
for diction in text:
text = text.replace(i, j)
Encryption(text,diction)
完整的错误消息:
^{pr2}$名称错误:未定义名称“text” 我试图让程序读取用户的输入,并将其转换为备用集。然而,这是行不通的。 我无法确定这个“命名”问题的来源,因为我以前尝试过定义文本,但它不起作用。在
我也没有发现除了hacks之外对定义顺序的充分解释。在
谢谢!在
你想这么做吗?在
def encrypt(toEncrypt)
变量toEncrypt
是参数或输入d = { ... }
是字典d['a']
将返回x1
,d['b']
将返回{temp = toEncrypt.lower()
将temp
设置为toEncrypt
的小写版本toReturn = ""
在for循环之外定义一个新的空白字符串for l in temp:
循环遍历temp
中的每个字符toReturn += d[l]
l
用作字典d
的键并返回相应的值return toReturn
是返回函数。。。或输出相关问题 更多 >
编程相关推荐