Beginner Python: AttributeError: 'list' object has no attribute
Beginner Python: AttributeError: 'list' object has no attribute
错误说:
AttributeError: 'list' object has no attribute 'cost'
我正在尝试使用以下 class 处理自行车字典来进行简单的利润计算:
class Bike(object):
def __init__(self, name, weight, cost):
self.name = name
self.weight = weight
self.cost = cost
bikes = {
# Bike designed for children"
"Trike": ["Trike", 20, 100],
# Bike designed for everyone"
"Kruzer": ["Kruzer", 50, 165]
}
当我尝试使用我的 for 语句计算利润时,出现属性错误。
# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
profit = bike.cost * margin
首先,我不知道为什么它指的是一个列表,而且一切似乎都被定义了,不是吗?
在这样使用之前,您需要将字典的值传递给 Bike
构造函数。或者,请参阅 namedtuple
-- 似乎更符合您要执行的操作。
考虑:
class Bike(object):
def __init__(self, name, weight, cost):
self.name = name
self.weight = weight
self.cost = cost
bikes = {
# Bike designed for children"
"Trike": Bike("Trike", 20, 100), # <--
# Bike designed for everyone"
"Kruzer": Bike("Kruzer", 50, 165), # <--
}
# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
profit = bike.cost * margin
print(profit)
输出:
33.0
20.0
不同之处在于,在您的 bikes
字典中,您将值初始化为列表 [...]
。相反,您的其余代码看起来需要 Bike
个实例。所以创建 Bike
个实例:Bike(...)
.
至于你的错误
AttributeError: 'list' object has no attribute 'cost'
当您尝试在 list
对象上调用 .cost
时会发生这种情况。非常简单,但我们可以通过查看您调用 .cost
的位置来弄清楚发生了什么——在这一行中:
profit = bike.cost * margin
表示至少有一个bike
(即bikes.values()
的成员是一个列表)。如果查看定义 bikes
的位置,您会发现这些值实际上是列表。所以这个错误是有道理的。
但是由于 你的 class 有一个成本属性,看起来你试图使用 Bike
个实例作为值,所以我做了一点变化:
[...] -> Bike(...)
一切就绪。
它们是列表,因为您在字典中将它们键入为列表:
bikes = {
# Bike designed for children"
"Trike": ["Trike", 20, 100],
# Bike designed for everyone"
"Kruzer": ["Kruzer", 50, 165]
}
你应该改用自行车-class:
bikes = {
# Bike designed for children"
"Trike": Bike("Trike", 20, 100),
# Bike designed for everyone"
"Kruzer": Bike("Kruzer", 50, 165)
}
这将使您能够像您尝试的那样获得 bike.cost 自行车的成本。
for bike in bikes.values():
profit = bike.cost * margin
print(bike.name + " : " + str(profit))
现在将打印:
Kruzer : 33.0
Trike : 20.0
错误说:
AttributeError: 'list' object has no attribute 'cost'
我正在尝试使用以下 class 处理自行车字典来进行简单的利润计算:
class Bike(object):
def __init__(self, name, weight, cost):
self.name = name
self.weight = weight
self.cost = cost
bikes = {
# Bike designed for children"
"Trike": ["Trike", 20, 100],
# Bike designed for everyone"
"Kruzer": ["Kruzer", 50, 165]
}
当我尝试使用我的 for 语句计算利润时,出现属性错误。
# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
profit = bike.cost * margin
首先,我不知道为什么它指的是一个列表,而且一切似乎都被定义了,不是吗?
在这样使用之前,您需要将字典的值传递给 Bike
构造函数。或者,请参阅 namedtuple
-- 似乎更符合您要执行的操作。
考虑:
class Bike(object):
def __init__(self, name, weight, cost):
self.name = name
self.weight = weight
self.cost = cost
bikes = {
# Bike designed for children"
"Trike": Bike("Trike", 20, 100), # <--
# Bike designed for everyone"
"Kruzer": Bike("Kruzer", 50, 165), # <--
}
# Markup of 20% on all sales
margin = .2
# Revenue minus cost after sale
for bike in bikes.values():
profit = bike.cost * margin
print(profit)
输出:
33.0 20.0
不同之处在于,在您的 bikes
字典中,您将值初始化为列表 [...]
。相反,您的其余代码看起来需要 Bike
个实例。所以创建 Bike
个实例:Bike(...)
.
至于你的错误
AttributeError: 'list' object has no attribute 'cost'
当您尝试在 list
对象上调用 .cost
时会发生这种情况。非常简单,但我们可以通过查看您调用 .cost
的位置来弄清楚发生了什么——在这一行中:
profit = bike.cost * margin
表示至少有一个bike
(即bikes.values()
的成员是一个列表)。如果查看定义 bikes
的位置,您会发现这些值实际上是列表。所以这个错误是有道理的。
但是由于 你的 class 有一个成本属性,看起来你试图使用 Bike
个实例作为值,所以我做了一点变化:
[...] -> Bike(...)
一切就绪。
它们是列表,因为您在字典中将它们键入为列表:
bikes = {
# Bike designed for children"
"Trike": ["Trike", 20, 100],
# Bike designed for everyone"
"Kruzer": ["Kruzer", 50, 165]
}
你应该改用自行车-class:
bikes = {
# Bike designed for children"
"Trike": Bike("Trike", 20, 100),
# Bike designed for everyone"
"Kruzer": Bike("Kruzer", 50, 165)
}
这将使您能够像您尝试的那样获得 bike.cost 自行车的成本。
for bike in bikes.values():
profit = bike.cost * margin
print(bike.name + " : " + str(profit))
现在将打印:
Kruzer : 33.0
Trike : 20.0