Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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
Can';t转换为';int';对象隐式地指向str:python3+;_Python_Int_Typeerror - Fatal编程技术网

Can';t转换为';int';对象隐式地指向str:python3+;

Can';t转换为';int';对象隐式地指向str:python3+;,python,int,typeerror,Python,Int,Typeerror,我的代码应该在输入后确定并显示二叉树的数量。 我一直收到无法将int对象隐式转换为str的错误,我不知道如何修复它。它很容易在3.0以下的Python版本中工作,所以请提供帮助,因为我仍然是Python的初学者,我想了解我做错了什么 import sys print ("Welcome to Binary Tree Enumeration!") x = input("Type an integer to output its binary trees: ") print ("\nYou ente

我的代码应该在输入后确定并显示二叉树的数量。
我一直收到
无法将int对象隐式转换为str的错误,我不知道如何修复它。它很容易在3.0以下的Python版本中工作,所以请提供帮助,因为我仍然是Python的初学者,我想了解我做错了什么

import sys
print ("Welcome to Binary Tree Enumeration!")
x = input("Type an integer to output its binary trees: ")
print ("\nYou entered " + str(x))
def distinct(x):
     leafnode = '.'
     dp = []
     newset = set()
     newset.add(leafnode)
     dp.append(newset)
     for i in range(1,x):
         newset = set()
         for j in range(i):
             for leftchild in dp[j]:
                 for rightchild in dp[i-j-1]:
                     newset.add(("(") + leftchild + rightchild + (")"))
         dp.append(newset)
     return dp[-1]
 alltrees = distinct(x+1)
 for tree in alltrees:
     print (tree)
 print ("Thank you for trying this out!")
我忘了添加…这是我得到的错误

Traceback (most recent call last):
  File "main.py", line 29, in 
    alltrees = distinct(x+1)
TypeError: Can't convert 'int' object to str implicitly

为了连接字符串和各种类型的字符串表示,必须将后者显式转换为字符串,例如。g

"(" + str(leftchild) + ", " + str(rightchild) + ")"
或者,更容易理解

"(%i, %i)" % (leftchild, rightchild)

默认情况下,当您使用
input
时,它始终是字符串输入

x = input("Type an integer to output its binary trees: ")
print ("\nYou entered " + str(x))
因此,没有必要再次转换它

这里使用
.format()

但是上面的一个不会维护数据结构

如果要保留数据结构,请使用

newset.add(tuple(leftchild, rightchild))

正如其他人所建议的,这来自您对
input
的调用。在Python27中:

>>> input() + 1
3 # I entered that
4
但是使用
raw_input()
(其行为与Python3+中的
input
相同):


您得到的错误至少应该表明错误发生在哪一行,包括这一行可能会有所帮助,因为到目前为止,答案都集中在
新闻集。添加
行,但我怀疑错误发生在
distinct(x+1)
是的,对不起,我忘记添加了。这是我得到的错误。。。回溯(最后一次调用):文件“main.py”,第29行,在alltrees=distinct(x+1)TypeError中:无法将“int”对象隐式转换为str您应该编辑问题以包含该信息。人们希望编辑他们的答案,使其更具体地反映实际错误(尽管这与他们已经说过的是一样的),并将其添加到其中,谢谢你的提示。使用格式添加硬编码的参数不是有点难看吗?为什么它们不是格式字符串的一部分?@spoonmiser,你是对的,如果你觉得这有点难看,你可以尝试使用
tuple()
instedi想得更多的
“({0}{1})”.format(leftchild,rightchild)
,只是在格式字符串中包含paren。但是,是的,根据数据的使用方式,元组可能是一个好主意。将字符串与
+
连接起来非常感谢您的建议,但它仍然不能解决我遇到的错误。我以前忘了加上它,所以它现在被包括在我的问题中。哇,这确实帮助我更好地理解它,我现在能够解决它了。非常感谢你!
>>> input() + 1
3 # I entered that
4
>>> raw_input() + 1
3 # I entered that
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> x = raw_input()
3
>>> type(x)
<type 'str'>
>>> x = int(input())
...