Python 获取 TypeError:只能将 str(而不是“int”)连接到 str
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51252580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a TypeError: can only concatenate str (not "int") to str
提问by 9ae
I am new to coding so I decided to make some kind of secret code for testing purposes with unicode, I've done that by adding numbers to Unicode so it would be kinda secret. I've been getting this error but I don't know how to solve it. Is there any solution? Here is code:
我是编码新手,所以我决定使用 unicode 制作某种用于测试目的的秘密代码,我已经通过向 Unicode 添加数字来做到这一点,所以它会有点秘密。我一直收到此错误,但我不知道如何解决。有什么解决办法吗?这是代码:
while True:
try:
message = int(input("Enter a message you want to be decrypt: "))
break
except ValueError:
print("Error, it must be an integer")
secret_string = ""
for char in message:
secret_string += chr(ord(char - str(742146))
print("Decrypted", secret_string)
q = input("")
回答by Alireza
Pythonworking a bit differently to JavaScript for example, the value you are concatenating needs to be same type, both intor str...
Python 的工作方式与 JavaScript 有点不同,例如,您连接的值需要是相同的类型,无论是int还是str...
So for example the code below throw an error:
例如,下面的代码抛出一个错误:
print( "Alireza" + 1980)
like this:
像这样:
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
print( "Alireza" + 1980)
TypeError: can only concatenate str (not "int") to str
To solve the issue, just add str to your number or value like:
要解决此问题,只需将 str 添加到您的数字或值中,例如:
print( "Alireza" + str(1980))
And the result as:
结果如下:
Alireza1980
回答by Abhishek Kashyap
instead of using " + " operator
而不是使用“+”运算符
print( "Alireza" + 1980)
Use comma " , " operator
使用逗号“,”运算符
print( "Alireza" , 1980)
回答by Tori Harris
Change secret_string += str(chr(char + 7429146))
改变 secret_string += str(chr(char + 7429146))
To secret_string += chr(ord(char) + 7429146)
到 secret_string += chr(ord(char) + 7429146)
ord()
converts the character to its Unicode integer equivalent. chr()
then converts this integer into its Unicode character equivalent.
ord()
将字符转换为其等效的 Unicode 整数。chr()
然后将此整数转换为其等效的 Unicode 字符。
Also, 7429146 is too big of a number, it should be less than 1114111
另外,7429146 太大了,应该小于 1114111
回答by Muhammad Ali
Use this:
用这个:
print("Program for calculating sum")
numbers=[1, 2, 3, 4, 5, 6, 7, 8]
sum=0
for number in numbers:
sum += number
print("Total Sum is: %d" %sum )
回答by InAFlash
Problem is you are doing the following
问题是您正在执行以下操作
str(chr(char + 7429146))
where char is a string. You cannot add a int with a string. this will cause that error
其中 char 是一个字符串。您不能添加带有字符串的 int。这将导致该错误
maybe if you want to get the ascii code and add it with a constant number. if so , you can just do ord(char) and add it to a number. but again, chr can take values between 0 and 1114112
也许如果您想获取 ascii 代码并将其添加为常数。如果是这样,您可以执行 ord(char) 并将其添加到数字中。但同样, chr 可以取 0 到 1114112 之间的值