2017-10-18 75 views
-1

我是一般的Python和编程新手。我试图对消息进行编码和解码,但发生了这种错误。这是什么意思?如何解决它?Str对象不可调用。编码和解码消息

Def main():  
    Mes=input(" enter the message to encode").  
    Key= int(input(" enter the key").  
    Print(chr(ord(chr)+key) for chr in mes)  
main()     
+1

为什么命名您的迭代变量'chr'试图使用内置在函数'chr'?使用不同的名称,如'char'或'c'。 –

+1

定义函数的正确语法是“def”而不是“Def”。确保您使用的是正确的大写。 –

+1

不要说谎。该代码不会产生该错误。在甚至可以运行该代码之前,您需要修复三个或四个语法错误。 –

回答

0

既然你已经写了for chr in mes,你有阴影内置名chr。现在,chr引用你的字符串,而不是python函数。喜欢的东西替代它:

chr(ord(mychar)+key) for mychar in mes 
+0

谢谢!现在它可以工作,但打印出来: at 0x0142FC00>而不是编码信息 – Krokonoxx

+0

如果你想将它打印成列表,你需要将表达式打印在'[]'中。如果一个表达式在括号内,它会创建一个*生成器*(查看它) –

+0

谢谢你的帮助 – Krokonoxx

0

此代码是我对你的程序版本

def main():  
    Mes=input(" enter the message to encode ")  
    Key= int(input(" enter the key "))  
    for ch in Mes: 
      print(chr(ord(ch)+Key))   
main() 

输出

enter the message to encode me 
enter the key 2 
o 
g