为什么python不返回None?

为什么python不返回None?,python,python-3.x,Python,Python 3.x,为什么在create_tree()中x==-1时不返回None 样本输出: class tree : def __init__(self): self.val=0 self.right=None self.left=None def create_tree(): x=input() if x==-1: return None root=tree() root.val=x

为什么在create_tree()中x==-1时不返回None

样本输出:

class tree :

    def __init__(self):

        self.val=0
        self.right=None
        self.left=None

def create_tree():

    x=input()

    if x==-1:
        return None

    root=tree()
    root.val=x

    print(root)
    print(root.val)
    print(root.right)
    print(root.left)



    while(True):
        print ("reach1")
        root.left=create_tree()
        root.right=create_tree()
        print("reach2")
        break


    return root

def main():

    root=tree()
    root=create_tree()

main()
2无无到达1
-1 
-1无达到1
为什么在create_tree()中x==-1时不返回None

因为
input()
stdin
输入返回一个字符串。
input
从输入中读取一行,将其转换为字符串并返回该字符串

您可以使用
类型
运算符检查此项

2 <__main__.tree object at 0x7f58cbf11128> 2 None None reach1
-1 <__main__.tree object at 0x7f58cbf11208>
-1 None None reach1
解决方案是将您输入的内容与
“-1”

或者只使用
int
方法

if x == "-1":
    return None

因为
-1!='-1'
@IshaanKalsi,如果您发现这回答了您的问题,我鼓励您按下绿色复选标记。
if x == "-1":
    return None
x = int(input())
if x == -1:
    return None