Python 类方法TypeError“;Int对象不可调用";

Python 类方法TypeError“;Int对象不可调用";,python,typeerror,Python,Typeerror,TypeError:“int”对象不可调用 class Car(): def __init__(self, make, model, year): """initialize attribuites to describe a car""" self.make = make self.model = model self.year = year self.odometer_reading = 0

TypeError:“int”对象不可调用

class Car():    

    def __init__(self, make, model, year):
        """initialize attribuites to describe a car"""
        self.make = make 
        self.model = model
        self.year = year
        self.odometer_reading = 0
        self.increment_odometer = 0

    def update_odometer(self, mileage):
        """
        Set the odometer reading to the given value. 
        Reject the change if it attempts to roll the odometer back.
        """
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You cannot roll back the odometer.") 

    def read_odometer(self):
        """print at statement which shows the car's miles"""
        print("This car has " + str(self.odometer_reading) + " " + "miles on it.")

    def get_descriptive_name(self):   
        """Return a neatly formatted descriptive name."""
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()


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


my_old_car = Car ('subaru', 'outback', 2013)
print (my_old_car.get_descriptive_name())

my_old_car.update_odometer(23500)
my_old_car.read_odometer()

my_old_car.increment_odometer(100)
my_old_car.read_odometer()
输出:

2013 Subaru Outback
This car has 23500 miles on it.
Traceback (most recent call last):
  File "cars.py", line 42, in <module>
    my_old_car.increment_odometer(100)
TypeError: 'int' object is not callable
2013斯巴鲁内陆
这辆车有23500英里。
回溯(最近一次呼叫最后一次):
文件“cars.py”,第42行,在
我的旧车。递增里程表(100)
TypeError:“int”对象不可调用

如前所述,您可以在
\uuuu init\uuuu
中定义增量里程表对象的名称,然后定义具有相同名称的方法。只需在
\uuuu init\uuuu

class Car():

    def __init__(self, make, model, year):
        """initialize attribuites to describe a car"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0
        # self.increment_odometer = 0 ----> remove this line

    def update_odometer(self, mileage):
        """
        Set the odometer reading to the given value.
        Reject the change if it attempts to roll the odometer back.
        """
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You cannot roll back the odometer.")

    def read_odometer(self):
        """print at statement which shows the car's miles"""
        print("This car has " + str(self.odometer_reading) + " " + "miles on it.")

    def get_descriptive_name(self):
        """Return a neatly formatted descriptive name."""
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

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


my_old_car = Car ('subaru', 'outback', 2013)
print (my_old_car.get_descriptive_name())

my_old_car.update_odometer(23500)
my_old_car.read_odometer()

my_old_car.increment_odometer(100)
my_old_car.read_odometer()

如前所述,您在
\uuuu init\uuuu
中定义了增量里程表对象的名称,然后定义了具有相同名称的方法。只需在
\uuuu init\uuuu

class Car():

    def __init__(self, make, model, year):
        """initialize attribuites to describe a car"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0
        # self.increment_odometer = 0 ----> remove this line

    def update_odometer(self, mileage):
        """
        Set the odometer reading to the given value.
        Reject the change if it attempts to roll the odometer back.
        """
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You cannot roll back the odometer.")

    def read_odometer(self):
        """print at statement which shows the car's miles"""
        print("This car has " + str(self.odometer_reading) + " " + "miles on it.")

    def get_descriptive_name(self):
        """Return a neatly formatted descriptive name."""
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

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


my_old_car = Car ('subaru', 'outback', 2013)
print (my_old_car.get_descriptive_name())

my_old_car.update_odometer(23500)
my_old_car.read_odometer()

my_old_car.increment_odometer(100)
my_old_car.read_odometer()

你有一个同名的方法和属性。你有一个同名的方法和属性。谢谢你,布鲁诺!我感谢所有对像我这样的程序员的帮助,不客气。我相信你会掌握窍门的。你的编程风格看起来不错,学习课程非常重要。若你们愿意,请你们将问题标记为已回答。谢谢,谢谢你,布鲁诺!我感谢所有对像我这样的程序员的帮助,不客气。我相信你会掌握窍门的。你的编程风格看起来不错,学习课程非常重要。若你们愿意,请你们将问题标记为已回答。谢谢