0

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

1
  • You have no such print_info method defined in your class. Commented Nov 27, 2022 at 23:21

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

Comments

-1
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

Welcome to Stack Overflow. Please read How to Answer. In particular, please make sure to address the question being asked when writing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.