Python帮助: 'str' object is not callable

标签 python

我正在学习 Python,在我的代码中遇到此错误。在网上搜索,但没有找到我理解的答案。有人可以帮忙吗?

类型错误:“str”对象不可调用

这是我的代码:

while True:
    print("Enter 'add' to add two numbers")
    print("Enter 'quit' to quit the program")
    user_input = input(": ")

    if user_input == "quit":
        break
    elif user_input == "add":
        num1 = float(input("Enter 1st number: "))
        num2 = float(input("Enter 2nd number: "))
        result = str(num1 + num2)
        print("Answer is: " + result)

最佳答案

这就是当您使用内置函数定义变量时会发生的情况:

>>> str = "string"
>>> str(12)
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: 'str' object is not callable

这种str赋值必须在这段代码之前执行,这是可以的。快速修复:

del str

正确修复:找到该变量的定义位置并重命名/删除它。

(请注意,inputfloat 可能是已重新定义的名称)

关于Python帮助: 'str' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983907/

相关文章:

python - 为什么我的代码每次运行后都会从同一图像中的同一列表中找到更多对象?

python - 如何确保只在 virtualenv 中调用 pip?

python - 来自 python 2.7.6 和 3.3.3 的不同递归结果

python - 在反向代理(traefik)后面使用 phyton 的 http.server 时获取 '404'

Python/PySpark 解析带有编号属性的 JSON 字符串

python - cygdb导入错误: No module named 'Cython'

python - Pandas - 通过获取其他列的行差异来创建新列

python - 不使用 SUM 函数的嵌套列表总和(练习)

python - 我需要帮助将 urlpatterns url 转换为等效路径

Python 请求有时会卡住