Class definition
class Car:
amount_cars = 0
def __init__(self, manufacturer, model, hp):
self.manufacturer = manufacturer
self.model = model
self.hp = hp
Car.amount_cars += 1
def print_car_amount(self):
print("Amount: {}".format(Car.amount_cars))
Creating instance
myCar1 = Car("Tesla", "Model X", 525)
Printing instance
myCar1.print_info()
Output:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [37], in <cell line: 1>()
----> 1 myCar1.print_info()
AttributeError: 'Car' object has no attribute 'print_info
Need help in finding the error
swiss_knight
8,39115 gold badges66 silver badges120 bronze badges
2 Answers 2
As it is stated in the error message, tou have no method by the name print_info. Probably, you're trying to do:
myCar1.print_car_amount()
Chris
139k139 gold badges317 silver badges293 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
class Car:
amount_cars = 0
def __init__(self, manufacturer, model, hp):
self.manufacturer = manufacturer
self.model = model
self.hp = hp
Car.amount_cars += 1
def print_info(self): # Changed
print("Amount: {}".format(Car.amount_cars))
1 Comment
Chris
Welcome to Stack Overflow. Please read How to Answer. In particular, please make sure to address the question being asked when writing answers.
lang-py
print_infomethod defined in your class.