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

swiss_knight
8,39115 gold badges66 silver badges120 bronze badges
asked Nov 27, 2022 at 6:48
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()
Chris
139k139 gold badges317 silver badges293 bronze badges
answered Nov 27, 2022 at 6:52
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))
answered Nov 27, 2022 at 7:44

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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.