Skip to content
Advertisement

>

Error message that I get is as below:

PS C:Python Tutorials> & C:/Python311/python.exe "c:/Python Tutorials/C_and_O_Electric_Car.py"
<bound method Car.get_descriptive_name of <__main__.ElectricCar object at 0x0000021A168CFC90>>

Source Code :

class Car:
    """A simple attempt to represent a car """

    def __init__(self,make,model,year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_descriptive_name(self):
        """Return a neatly formatted descriptive name"""
        long_name = f" {self.year} {self.make} {self.model}"
        return long_name.title()
    
    def read_odometer(self):
        """Print a statement showing the car's milage."""
        print(f"This car has {self.odometer_reading} miles on it")

    def update_odometer(self, milage):
        """Set the odometer value to the given value 
        Reject the change it it tries to roll the odometer back"""
        if milage > self.odometer_reading:
            self.odometer_reading = milage
        else:
            print("You cant rollback the odometer!")

    def increment_odometer(self,miles):
        """Add the give amount to the odometer reading"""
        self.odometer_reading += miles

class ElectricCar(Car):
        def __init__(self, make, model, year):
            super().__init__(make,model,year)

my_tesla = ElectricCar('Tesla','Model S', 2019)
print(my_tesla.get_descriptive_name)

Advertisement

Answer

This is not an error. It’s printing the correct thing. What you are doing is not calling a function instead printing it as an object as in python everything is an object.

Solution:

print(my_tesla.get_descriptive_name())

This will print your desired output i.e “2019 Tesla Model S”

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement