在 Python 3.x 中出错:TypeError: 'int' object is not callable

我是编程新手。我从 Python 3.x 开始,从 Python Crash Course 学习。我在尝试书中的示例程序时遇到错误。代码是:

class 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):
        car_name = str(self.year) + ' ' + self.make + ' ' + self.model + 'model.'
        return car_name.title()
    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")

my_dream_car = Car('lamborghini', 'one-off', 2017 )
print("\nMy dream car is " + my_dream_car.get_descriptive_name())
my_dream_car.odometer_reading = 23
my_dream_car.odometer_reading()

输出:

Traceback (most recent call last):

  File "C:/Users/Jai/PycharmProjects/PCC/Chapter_9-Classes/4_car_class.py", line 18, in <module>

My dream car is 2017 Lamborghini One-Off Model.

    my_dream_car.odometer_reading()

TypeError: 'int' object is not callable

进程以退出代码 1 结束

西蒙·维瑟

您将 的值设置为my_dream_car.odometer_reading整数,然后调用它:

my_dream_car.odometer_reading = 23
my_dream_car.odometer_reading()

这就是您得到的TypeError: 'int' object is not callable原因,因为int它不是您可以调用的。

你可能想写:

my_dream_car.odometer_reading = 23
my_dream_car.read_odometer()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python - 有没有办法将函数放入函数而不是“TypeError:'int' object is not callable”?

为什么我在 python(discord bot) 中出现以下错误:TypeError: 'str' object is not callable

尝试通过索引获取元组中的对象时,Python 中出现“TypeError: 'tuple' object is not callable”错误

'TypeError: 'int' object is not callable' 比较相隔 5 秒的读数时

我如何解决这个 TypeError: 'int' object is not callable?

对于 i in range(len(data)): TypeError: 'int' object is not callable

在 Python 中出现错误`SyntaxWarning: 'str' object is not callable`

使用 __getattr__ 导致 TypeError: 'str' object is not callable - Python 2.7

如何修复 Python 中的“TypeError: 'str' object is not callable”错误?

Python:如何解决“TypeError:'str' object is not callable”

如何使用 SQLite 在 python 3.x 中修复“'int' object has no attribute 'execute'”错误?

为什么我会收到 TypeError: 'int' object not callable in a micro:bit MicroPython 程序?

在 INSERT INTO table [Python] [Flask] [sqlite3] 之后您可能会收到“TypeError: 'str' object is not callable”的另一个原因

Python/MySQL 在 for 中执行给出错误:- TypeError: 'int' object is not iterable

我不断收到 TypeError: 'str' object is not callable

Python tkinter Entry 小部件验证错误:TypeError:'str' object is not callable

如何在 Python 中解决此错误:“TypeError: ‘str’ object is not callable”

获取 TypeError: 'numpy.float64' object is not callable 错误

TypeError: 'DataFrame' object is not callable error when using seaborn pairplot ?

'TypeError: 'numpy.ndarray' object is not callable' 是什么意思?

我在 tkinter 中收到此错误:TypeError: 'Button' object is not callable

有人可以帮我 TypeError: 'str' object is not callable

range() 函数产生错误 'TypeError: 'str' object is not callable'

为什么会显示“TypeError: 'Label' object is not callable”?

ValueError:int() 的无效文字,基数为 10:'' from Python 2.X to 3.X

information class part shows an error==>bound method error & int object is not callable"

No TypeError在Python3中比较int与None

[python-3] TypeError:必须为str,而不是int

TypeError:“ int”对象在Python3中不可下标