我自學Python使用How to Think Like a Computer Scientist
。我從文本中複製了這段代碼,但由於某些原因,我的代碼出現了錯誤。任何人都可以看到我失蹤的東西嗎?爲什麼Python說print_list
沒有定義?節點對象不能正常工作的打印方法
class Node:
def __init__(self, cargo = None, next = None):
self.cargo = cargo
self.next = next
def __str__(self):
return str(self.cargo)
def print_list(node):
while node:
print node,
node = node.next
print
以下是錯誤:
==== RESTART: /Desktop/Programming Career/Untitled.py ====
Traceback (most recent call last):
File "Users/Desktop/Programming Career/Untitled.py", line 24, in <module>
print_list(node1)
NameError: name 'print_list' is not defined
我試圖定義這樣的print_list:
def print_list(self):
for node in self:
print node,
node = node.next
print
,但我得到了同樣的錯誤: NameError: name 'print_list' is not defined
將'print_list'移出'Node'的定義。 – ForceBru
您的代碼縮進存在問題,請修復它,以便我們爲您提供幫助! – Arman
Unindent函數print_list()不是類Node的一部分。 –