Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我收到此错误“TypeError:无法将'int'对象隐式转换为str”_Python_Python 3.x_Typeerror - Fatal编程技术网

Python 我收到此错误“TypeError:无法将'int'对象隐式转换为str”

Python 我收到此错误“TypeError:无法将'int'对象隐式转换为str”,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,我收到以下错误类型错误:无法将'int'对象隐式转换为str plaintext= input("Enter the plaintext:") length = len(plaintext) print ("The length is:", length) for i in range(0,(len(plaintext)-1) E[i]= (15*plaintext[i]+20)%26; print (E[i]) 明文是字符串,明文[i]也是字符串。15*纯文本[i]将字符串相

我收到以下错误类型错误:无法将'int'对象隐式转换为str

plaintext= input("Enter the plaintext:")
length = len(plaintext)
print ("The length is:", length)
for i in range(0,(len(plaintext)-1)
    E[i]= (15*plaintext[i]+20)%26;
    print (E[i])
明文是字符串,明文[i]也是字符串。15*纯文本[i]将字符串相乘,然后尝试将整数添加到+20的字符串中。所以只要:

15*int(plaintext[i])+20 #if plaintext is a string of decimals, don't know what exactly you want
或者我猜你在做一些类似于加密的事情,这样你需要使用将单个字符转换为整数,然后使用它的倒数进行转换:

明文是str,所以明文[i]是字符串,也是一个字符。将它乘以15,你又得到了一个15个字符的str。如果您尝试向其中添加一个20,解释器会假定您希望将该20转换为str并将其附加到现有str。但它不会执行从int到str的隐式转换,它会告诉您这一点

你可能想用像这样的东西

(15 * (ord(plaintext[i]) - ord('A')) + 20) % 26

您的问题不清楚您的真实意图,因此我们只能猜测。

您能否提供完整的回溯。。。我想你应该得到一个错误:在任何事情之前:这让我想知道这是否是正在运行的实际代码…确切地说,我需要从用户那里得到明文,我需要加密,然后我需要转换为十进制,然后再将其包含在方程Ex=15x+20mod 26中,其中x是明文。上述方程式应逐字应用。最后,我需要再次转换回字符串。如何将输入的明文转换为十进制,从A=0到Z=25。从纯文本中提取每个字符并应用该等式,最后将所得整数再次转换回字符串。@AndrewNiteesh,如何?我已经给你看了:ordch-97。此外,下次提问时,最好将准确的问题公式、样本输入和输出像下面的评论一样张贴出来;import sys plaintext=输入明文:长度=lenplaintext打印长度为:,i的长度范围为0,lenplaintext:c=chr15*ordplaintext-97+20%26+97打印上述代码的c,在输出中,我得到一个类似这样的错误:输入明文:andrew长度是:6回溯最近的调用last:文件C:\Python33\affinecipher.py,第6行,C=chr15*ordplaintext-97+20%26+97类型错误:ord需要一个字符,但是长度为6的字符串found@AndrewNiteesh请注意,我的ch是一个在文本中循环的单个字符。你可以使用ord'a',但不能使用ord'abc',我需要从用户那里得到明文,我需要加密,然后我需要转换成十进制,然后再将其包含在方程Ex=15x+20mod 26中,其中x是明文。上述方程式应逐字应用。最后,我需要再次转换回字符串。如何将输入的明文转换为十进制,从A=0到Z=25。从纯文本中提取每个字符并应用该等式,最后将所得整数再次转换回字符串。请帮我解决这个问题。我根据您的新规格更改了答案。不过,它不包含任何检查,以确保输入仅包含从A到Z的字母。
(15 * (ord(plaintext[i]) - ord('A')) + 20) % 26