如何解决 TypeError: can only concatenate str (not "int") to str [英] How to resolve TypeError: can only concatenate str (not "int") to str
本文介绍了如何解决 TypeError: can only concatenate str (not "int") to str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我决定使用 Unicode 制作某种密码以进行测试.
- 我已经通过向 Unicode 添加数字来做到这一点,所以这将是一种秘密.
- 我一直收到此错误,但我不知道如何解决.
- 有什么解决办法吗?
message = input("Enter a message you want to be revealed: ") secret_string = "" for char in message: secret_string += str(chr(char + 7429146)) print("Revealed", secret_string) q = input("")
原始错误
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-182-49ece294a581> in <module> 2 secret_string = "" 3 for char in message: ----> 4 secret_string += str(chr(char + 7429146)) 5 print("Revealed", secret_string) 6 q = input("") TypeError: can only concatenate str (not "int") to str
更新代码
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("")
推荐答案
Python 与 JavaScript 的工作方式有点不同,例如,您连接的值需要是相同的类型,都是 int 或 str...
Python working a bit differently to JavaScript for example, the value you are concatenating needs to be same type, both int or str...
例如下面的代码抛出错误:
print( "Alireza" + 1980)
像这样:
Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> print( "Alireza" + 1980) TypeError: can only concatenate str (not "int") to str
要解决此问题,只需将 str 添加到您的数字或值中,例如:
To solve the issue, just add str to your number or value like:
print( "Alireza" + str(1980))
结果如下:
Alireza1980
这篇关于如何解决 TypeError: can only concatenate str (not "int") to str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文