函数在应该返回 int 时不返回任何内容
Function not returning anything when it should be returning int
所以我得到了这个函数,它应该接受一个 Optional[Node] 作为参数。 Node class 是递归的,接受 data: int 和 next: Optional[Node] 作为参数。我的函数的想法是 return 与链表关联的最后一个数据 int 值,并使用递归来这样做。
我的函数在完成时确实打印“Returns {head.data}”,但没有伴随 return 语句。它几乎就好像它从未到达 return 语句而只是在打印函数处停止。我完全被难住了。
这是节点 class:
class Node:
"""An item in a singly-linked list."""
data: int
next: Optional[Node]
def __init__(self, data: int, next: Optional[Node]):
"""Construct a singly linked list. Use None for 2nd argument if tail."""
self.data = data
self.next = next
def __repr__(self) -> str:
"""Produce a string representation of a linked list."""
if self.next is None:
return f"{self.data} -> None"
else:
return f"{self.data} -> {self.next}"
以及有问题的函数:
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
else:
if head.next is None:
print(f"Returns {head.data}")
return head.data
else:
print("Calling last...")
last(head.next)
我的终端输出:
>>> last(Node(110, Node(210, Node(310, None))))
Calling last...
Calling last...
Returns 310
所以,函数应该是 return 310,但它不是。它只是打印出 print 语句并停在那里。有什么问题吗?是“if head is None:”这一行吗?虽然,它从不打印出“Returning None...”,所以这似乎不是问题所在。
您实际上忘记了 return 对 last
的递归调用的结果
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
else:
if head.next is None:
print(f"Returns {head.data}")
return head.data
else:
print("Calling last...")
return last(head.next) # Added return here
我相信你忽略了 return last(head.next)
。请注意,else
语句不是必需的,因为 return
语句会为您完成工作:
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
if head.next is None:
print(f"Returns {head.data}")
return head.data
print("Calling last...")
return last(head.next)
所以我得到了这个函数,它应该接受一个 Optional[Node] 作为参数。 Node class 是递归的,接受 data: int 和 next: Optional[Node] 作为参数。我的函数的想法是 return 与链表关联的最后一个数据 int 值,并使用递归来这样做。
我的函数在完成时确实打印“Returns {head.data}”,但没有伴随 return 语句。它几乎就好像它从未到达 return 语句而只是在打印函数处停止。我完全被难住了。
这是节点 class:
class Node:
"""An item in a singly-linked list."""
data: int
next: Optional[Node]
def __init__(self, data: int, next: Optional[Node]):
"""Construct a singly linked list. Use None for 2nd argument if tail."""
self.data = data
self.next = next
def __repr__(self) -> str:
"""Produce a string representation of a linked list."""
if self.next is None:
return f"{self.data} -> None"
else:
return f"{self.data} -> {self.next}"
以及有问题的函数:
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
else:
if head.next is None:
print(f"Returns {head.data}")
return head.data
else:
print("Calling last...")
last(head.next)
我的终端输出:
>>> last(Node(110, Node(210, Node(310, None))))
Calling last...
Calling last...
Returns 310
所以,函数应该是 return 310,但它不是。它只是打印出 print 语句并停在那里。有什么问题吗?是“if head is None:”这一行吗?虽然,它从不打印出“Returning None...”,所以这似乎不是问题所在。
您实际上忘记了 return 对 last
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
else:
if head.next is None:
print(f"Returns {head.data}")
return head.data
else:
print("Calling last...")
return last(head.next) # Added return here
我相信你忽略了 return last(head.next)
。请注意,else
语句不是必需的,因为 return
语句会为您完成工作:
def last(head: Optional[Node]) -> Optional[int]:
"""Returns the last value of a Linked List, or None if the list is empty."""
if head is None:
print("Returning None...")
return None
if head.next is None:
print(f"Returns {head.data}")
return head.data
print("Calling last...")
return last(head.next)