我是一个python /编程newb,我是自学python。不幸的是我真的卡住了!我在第34章"如何像计算机科学家一样思考"。 这里的对象是使用链表编写优先级队列ADT的实现。
我在第22行和第32行收到错误。我对此错误的理解是,我的一个或多个节点对象没有属性" cargo"。我不明白这一点,因为我的所有节点对象在创建时都被分配了一个" None"或某个数字。节点对象按照书中的说明编写。
我应该提一下,LINE 31的工作很精细。当我添加LINE 32时,我只会遇到一个问题,它会调用LINE 22(这显然是问题隐藏的地方),我收到错误。
任何见解都会有所帮助。非常感谢你!!!
我以较小的规模重现了这个问题:
class Node:
def __init__(self, cargo= None, next = None):
self.cargo = cargo
self.next = next
class Priority_Queue_For_Linked():
def __init__ (self):
self.max = None
self.nextmax = None
self.min = None
self.length = 0
def insert (self, cargo):
node = Node(cargo)
if self.length == 0:
node = self.max = self.min
self.length = 1
return
if node.cargo > self.max.cargo: # <--LINE 22
notmaxanymore = self.max
notmaxanymore.next = node
node = self.max
self.nextmax = notmaxanymore
self.length += 1
return
testlist = Priority_Queue_For_Linked()
testlist.insert(12)
testlist.insert(22) # <--LINE 32
这是错误:
=== RESTART: /Users/Desktop/Programming Career/Untitled6.py ===
Traceback (most recent call last):
File "/Users/Desktop/Programming Career/Untitled6.py", line 31, in <module>
testlist.insert(22)# <--line 32
File "/Users/Desktop/Programming Career/Untitled6.py", line 21, in insert
if node.cargo > self.max.cargo:# <--line 22
AttributeError: 'NoneType' object has no attribute 'cargo'
我怀疑你需要更多的上下文,但如果它有帮助,这里是所有的代码:
class Node:
def __init__(self, cargo= None, next = None):
self.cargo = cargo
self.next = next
def __str__(self):
return str(self.cargo)
def print_backward (self):
if self.next != None:
tail = self.next
tail.print_backward()
print self.cargo,
def print_list(node):
while node:
print node,
node = node.next
print
class Priority_Queue_For_Linked():
def __init__ (self):
self.max = None
self.nextmax = None
self.min = None
self.length = 0
def is_empty (self):
if self.length == 0:
print "List is empty."
def insert (self, cargo):
node = Node(cargo)
if self.length == 0:
node = self.max = self.min
self.length = 1
return
if node.cargo > self.max.cargo:########40
notmaxanymore = self.max
notmaxanymore.next = node
node = self.max
self.nextmax = notmaxanymore
self.length += 1
return
if node.cargo < self.min.cargo:
notminanymore = self.min
node.next = notminanymore
node = self.min
self.length += 1
return
else:
comparisonnode = self.min.next
comparisonprev = self.min
for i in range (2, self.length):
if node.cargo < comparisonnode.cargo:
node.next = comparisonnode
comparisonprev.next = node
self.length += 1
break
comparisonnode = comparisonnode.next
comparisonprev = comparisonprev.next
def remove (self):
maxtoremove = self.max
self.max = self.nextmax
self.max.next = None
length -=1
return maxtoremove.cargo
def print_list(self):
tempnode = self.min
print "[",
while not tempnode == None:
print tempnode,
tempnode = tempnode.next
print "]"
testlist = Priority_Queue_For_Linked()
testlist.insert(12)
testlist.insert(22)#######86
答案 0 :(得分:3)
当您插入第一个项目时,您的作业出现问题:
if self.length == 0:
node = self.max = self.min
self.length = 1
return
这不会更改self.max
或self.min
的值,它们将保持为None
,当然不会cargo
。self.max = self.min = node
您还将丢失要添加的节点。我假设你想这样做:
import random
play = 'y'
Original = 1
while play.lower() == 'y':
chance = random.randint(0,3)
if chance == 0:
print("Unlucky.... better luck next time")
else:
newnumber = Original*2
print (newnumber)
play = input("Play again?[y/n]: ")
答案 1 :(得分:1)
Priority_Queue_For_Linked类的public class PageClass{
public PageClass(WebDriver driver){
this.driver = driver;
}
public void testPage() {
driver.get("https://www.google.co.uk/");
}
}
public class MainTest {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","chromedriver.linux");
WebDriver driver = new ChromeDriver();
PageClass pageClassTest = new PageClass(driver);
pageClassTest.testPage();
}
}
属性在max
方法中定义为None,并且在if语句之前未被更改。
我认为您打算在某个时刻指定一个Node作为该属性的值,但您还没有这样做。
答案 2 :(得分:1)
您的代码尝试执行self.max.cargo
。在__init__
中,您将self.max
设置为无,并且您永远不会在其他任何位置更改此值,因此self.max
为无。如果您希望能够使用self.max
,则需要在某个时刻将其设置为实际节点值。