作为家庭作业,我必须以节点作为对象来执行链接列表,而节点将携带Car class
作为其数据。不确定我是否正确行事,但我的朋友们试图帮助我,这就是我所拥有的:
class Car:
def __init__(self, identification = None, name = None, brand = None,
price = None, active = None):
self.identification = identification
self.name = name
self.brand = brand
self.price = price
self.active = active
class Node:
def __init__(self, data):
self.prevNode = None
self.nextNode = None
self.data = data
class LinkedList:
def __init__(self):
self.head = None
def insertNode(self, car):
newNode = Node(car)
curNode = self.head
if self.head is None:
self.head = newNode
elif newNode.data.price < curNode.data.price:
newNode.nextNode = self.head
self.head.prevNode = newNode
self.head = newNode
else:
while curNode.nextNode is not None and curNode.nextNode.data.price <= newNode.data.price:
curNode = curNode.nextNode
newNode.nextNode = curNode.nextNode
curNode.nextNode = newNode
db = LinkedList()
def init(cars):
for car in cars:
db.insertNode(car)
def add(car):
db.insertNode(car)
def updateName(identification, name):
curNode = db.head
while curNode.data.id != identification:
curNode = curNode.next
curNode.data.name = name
def activateCar(identification):
curNode = db.head
while curNode.data.id != identification:
curNode = curNode.next
curNode.data.active = True
def calculateCarPrice():
total = 0
for i in range(db):
if db[i].data.active == True:
total += db[i].data.price
i += 1
return total
car1 = (1, 'a', 'audi', 3000, True)
car2 = (1, 'b', 'bmw', 5000, False)
cars = [car1, car2]
init(cars)
在我看来,一切都应该没问题,但是当我调用init
函数时它会返回此错误。
追踪(最近一次呼叫最后一次):
文件“C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”,第89行,
的init(汽车)
文件“C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”,第40行,在init
db.insertNode(汽车)
文件“C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”,第26行,在insertNode中
elif newNode.data.price&lt; curNode.data.price:
AttributeError:'tuple'对象没有属性'price'
我不明白为什么它甚至会返回'tuple
'..
答案 0 :(得分:0)
你写的地方
car1 = (1, 'a', 'audi', 3000, True)
car2 = (1, 'b', 'bmw', 5000, False)
应该说
car1 = Car(1, 'a', 'audi', 3000, True)
car2 = Car(1, 'b', 'bmw', 5000, False)