[英]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.