繁体   English   中英

AttributeError:“元组”对象没有属性“价格”

[英]AttributeError: 'tuple' object has no attribute 'price'

作为一项家庭作业,我必须以节点为对象创建一个链接列表,而节点将以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函数时,它将返回此错误。

追溯(最近一次通话):

在第89行中输入文件“ C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”

初始化(汽车)

init文件“ C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”,第40行

db.insertNode(汽车)

在insertNode中的文件“ C:/Users/Matyas/Desktop/ZAL/Cvičení/showroom.py”,第26行

elif newNode.data.price <curNode.data.price:

AttributeError:“元组”对象没有属性“价格”

我不明白为什么它甚至返回“ tuple ”。

你写的地方

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM