迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Python 错误 TypeError: Int Object Is Not Callable

作者:迹忆客 最近更新:2023/07/06 浏览次数:

这是使用 Python 编码时遇到的常见错误之一。 Python 可能在多种情况下引发此错误。

主要是当您在执行计算时错过数学运算符并在代码中使用内置函数作为变量时,就会发生这种情况。

本篇文章将讨论所有场景以及修复 TypeError: 'int' object is not callable in Python 的解决方案。


python 中添加缺少的运算符以修复 TypeError: 'int' object is not callable

有时您可能忘记在代码中添加数学运算符。 结果,你会得到一个 TypeError: 'int' object is not callable。

让我们以这个简单的 Python 脚本为例。

marks_obtained=450
total_marks=600
percentage=100(marks_obtained/total_marks)
print("The percentage is:", percentage)

输出:

Traceback (most recent call last):
  File "c:\Users\rhntm\myscript.py", line 3, in <module>
    percentage=100(marks_obtained/total_marks)
TypeError: 'int' object is not callable

它返回错误,因为百分比计算代码中缺少乘法运算符。 您可以通过在代码中添加乘法运算符 * 来解决此问题。

marks_obtained=450
total_marks=600
percentage=100*(marks_obtained/total_marks)
print("The percentage is:", percentage)

输出:

The percentage is: 75.0

Python 中更改变量名称以修复 TypeError: 'int' object is not callable

如果您使用变量的内置函数名称并稍后调用该函数,您将收到一条错误消息 'int' object is not callable

在下面的示例中,我们声明了一个 sum 变量,sum() 是 Python 中的内置函数,用于添加迭代器的项。

num=[2,4,6,8]
sum=0
sum=sum(num)
print("The sum is:", sum)

输出:

Traceback (most recent call last):
  File "c:\Users\rhntm\myscript.py", line 3, in <module>
    sum=sum(num)
TypeError: 'int' object is not callable

由于我们使用了 sum 变量,并随后调用了 sum() 函数来计算列表中数字的总和,因此它会引发 TypeError,因为 sum 变量覆盖了 sum() 方法。

您可以通过重命名变量名称来修复此类错误。 在这里,我们将变量 sum 更改为 Total。

num=[2,4,6,8]
total=0
total=sum(num)
print("The sum is:", total)

输出:

The sum is: 20

可以看到,这次代码运行成功了。

现在您知道如何修复 Python 中的 'int' object is not callable 错误。 我们希望这些解决方案对您有所帮助。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Python 中错误 Address Already in Use

发布时间:2023/07/09 浏览次数:173 分类:Python

我们将通过示例介绍Python中何时出现 Address already in use 错误以及如何解决。Python 中的错误Address already in use 本文将讲述运行使用端口的程序时发生的Python堆栈错误。

Python 中错误 ValueError: Math Domain Error

发布时间:2023/07/09 浏览次数:607 分类:Python

在本篇文章中,我们的目标是探索解决 Python 中的 ValueError: math domain error 错误的不同方法。当编码方面数学(基础或高级)的使用存在固有缺陷时,Python 中通常会引发 ValueError: math domain error 错

Python 错误 Name xrange Is Not Defined

发布时间:2023/07/09 浏览次数:153 分类:Python

本篇文章将介绍如何解决 Python 中 name 'xrange' is not defined 的错误。解决Python中name 'xrange' is not defined错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。

Python 中错误 AttributeError: __Enter__

发布时间:2023/07/09 浏览次数:2241 分类:Python

在 Python 中,AttributeError 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。

Python 错误 Error: Invalid Command Bdist_wheel

发布时间:2023/07/09 浏览次数:847 分类:Python

在 Python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 Python 中解决此问题的可能解决方案。安装wheel包来修复Python中 Error:invalid command 'bdist_

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便