[英]why does dict({4:None}) returns {4: None} but dict([{4,None}]) returns {None: 4} in Python 3.11
[英]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()
为什么在create_tree()中x ==-1时不返回None?
样本输出:
2 <__main__.tree object at 0x7f58cbf11128> 2 None None reach1
-1 <__main__.tree object at 0x7f58cbf11208>
-1 None None reach1
为什么在create_tree()中x ==-1时不返回None?
因为input()
从stdin
输入返回一个字符串 。 input
从input
读取一行,将其转换为字符串并将其返回。
您可以使用type
运算符对此进行检查。
type_of = type(x)
>> string
解决方案是将您输入的内容与"-1"
进行比较
if x == "-1":
return None
或只是使用int
方法。
x = int(input())
if x == -1:
return None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.