Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner Python: AttributeError: 'list' object has no attribute

The error says:

AttributeError: 'list' object has no attribute 'cost'  

I am trying to get a simple profit calculation to work using the following class to handle a dictionary of bicycles:

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]     } 

When I try to calculate profit with my for statement, I get the attribute error.

# Markup of 20% on all sales margin = .2 # Revenue minus cost after sale for bike in bikes.values():     profit = bike.cost * margin 

First, I don't know why it is referring to a list, and everything seems to be defined, no?

like image 204
Charles Watson Avatar asked Mar 29 '15 22:03

Charles Watson


People also ask

How do you fix an object that has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.

What does list object has no attribute mean Python?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

Has no attribute replace Python?

The Python "AttributeError: 'list' object has no attribute 'replace'" occurs when we call the replace() method on a list instead of a string. To solve the error, call replace() on a string, e.g. by accessing the list at a specific index or by iterating over the list.

What is attribute error in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.


1 Answers

Consider:

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) 

Output:

 33.0 20.0 

The difference is that in your bikes dictionary, you're initializing the values as lists [...]. Instead, it looks like the rest of your code wants Bike instances. So create Bike instances: Bike(...).

As for your error

AttributeError: 'list' object has no attribute 'cost' 

this will occur when you try to call .cost on a list object. Pretty straightforward, but we can figure out what happened by looking at where you call .cost -- in this line:

profit = bike.cost * margin 

This indicates that at least one bike (that is, a member of bikes.values() is a list). If you look at where you defined bikes you can see that the values were, in fact, lists. So this error makes sense.

But since your class has a cost attribute, it looked like you were trying to use Bike instances as values, so I made that little change:

[...] -> Bike(...) 

and you're all set.

like image 75
jedwards Avatar answered Sep 20 '22 05:09

jedwards