如何解决TypeError:只能将str(而不是“ int”)连接到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("")


慕的地8271018
浏览 531回答 5
5回答

慕容森

例如,Python的工作方式与JavaScript有所不同,您要连接的值必须是int或str相同的类型。因此,例如下面的代码抛出错误:print( "Alireza" + 1980)像这样:Traceback (most recent call last):&nbsp; File "<pyshell#12>", line 1, in <module>&nbsp; &nbsp; print( "Alireza" + 1980)TypeError: can only concatenate str (not "int") to str要解决此问题,只需将str添加到您的数字或值中,例如:print( "Alireza" + str(1980))结果为:Alireza1980

撒科打诨

而不是使用“ +”运算符print(&nbsp;"Alireza"&nbsp;+&nbsp;1980)使用逗号“”运算符print(&nbsp;"Alireza"&nbsp;,&nbsp;1980)

慕婉清6462132

使用f字符串来解决 TypeErrorf字符串:一种在Python中格式化字符串的新方法和改进方法PEP 498-文字字符串插值# the following line causes a TypeError# test = 'Here is a test that can be run' + 15 + 'times'# same intent with a f-stringi = 15test = f'Here is a test that can be run {i} times'print(test)# output'Here is a test that can be run 15 times'i = 15# t = 'test' + i&nbsp; # will cause a TypeError# should bet = f'test{i}'print(t)# output'test15'问题可能是试图评估表达式,其中变量是数字的字符串。将字符串转换为int。此方案特定于此问题进行迭代时,请务必注意 dtypei = '15'# t = 15 + i&nbsp; # will cause a TypeError# convert the string to intt = 15 + int(i)print(t)# output30笔记答案的前半部分解决了TypeError问题标题中显示的内容,这就是为什么人们似乎会问这个问题的原因。但是,这不能解决与OP提供的示例相关的问题,下面将解决该问题。原始代码问题TypeError是由于messagetype是造成的str。该代码会迭代每个字符,并尝试将char,str类型和int该问题可以通过转换char为int出现代码后,secret_string需要使用0而不是进行初始化""。该代码还会导致,ValueError: chr() arg not in range(0x110000)因为7429146超出的范围chr()。通过使用较小的数字来解决输出不是预期的字符串,这将导致问题中的更新代码。message = input("Enter a message you want to be revealed: ")secret_string = 0for char in message:&nbsp; &nbsp; char = int(char)&nbsp; &nbsp; value = char + 742146&nbsp; &nbsp; secret_string += ord(chr(value))&nbsp; &nbsp;&nbsp;print(f'\nRevealed: {secret_string}')# OutputEnter a message you want to be revealed:&nbsp; 999Revealed: 2226465更新的代码问题message现在是一种int类型,因此for char in message:原因TypeError: 'int' object is not iterablemessage转换为int以确保input是int。用 str()仅value使用chr不要使用 ordwhile True:&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; message = str(int(input("Enter a message you want to be decrypt: ")))&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; print("Error, it must be an integer")&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;secret_string = ""for char in message:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; value = int(char) + 10000&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; secret_string += chr(value)print("Decrypted", secret_string)# outputEnter a message you want to be decrypt:&nbsp; 999Decrypted ✙✙✙Enter a message you want to be decrypt:&nbsp; 100Decrypted ✑✐✐

忽然笑

改变&nbsp;secret_string += str(chr(char + 7429146))至&nbsp;secret_string += chr(ord(char) + 7429146)ord()将字符转换为其等效的Unicode整数。chr()然后将此整数转换为等效的Unicode字符。另外,7429146的个数太大,应小于1114111

qq_花开花谢_0

用这个:print("Program for calculating sum")numbers=[1, 2, 3, 4, 5, 6, 7, 8]sum=0for number in numbers:&nbsp; &nbsp; sum += numberprint("Total Sum is: %d" %sum )
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python