[英]Why python not returns 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()
Why None is not returned when x==-1 in create_tree() ? 为什么在create_tree()中x ==-1时不返回None?
sample output: 样本输出:
2 <__main__.tree object at 0x7f58cbf11128> 2 None None reach1
-1 <__main__.tree object at 0x7f58cbf11208>
-1 None None reach1
Why None is not returned when x==-1 in create_tree() ?
为什么在create_tree()中x ==-1时不返回None?
Because input()
returns a string from stdin
input. 因为
input()
从stdin
输入返回一个字符串 。 input
reads a line from input, converts it to a string and returns that. input
从input
读取一行,将其转换为字符串并将其返回。
You can check this using type
operator. 您可以使用
type
运算符对此进行检查。
type_of = type(x)
>> string
The solution is to compare what you've entered with "-1"
解决方案是将您输入的内容与
"-1"
进行比较
if x == "-1":
return None
or just use int
method. 或只是使用
int
方法。
x = int(input())
if x == -1:
return None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.