简体   繁体   English

Python帮助:'str'对象不可调用

[英]Python help: 'str' object is not callable

I am learning Python, getting this error in my code. 我正在学习Python,并在我的代码中遇到此错误。 Searched online, but didn't find an answer I understand. 在线搜索,但找不到我理解的答案。 Can someone help? 有人可以帮忙吗?

TypeError: 'str' object is not callable TypeError:“ str”对象不可调用

Here is my code: 这是我的代码:

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)

it's what happens when you're using built-in functions to define your variables: 当您使用内置函数定义变量时,会发生以下情况:

>>> 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

this kind of str assignment must have been performed before this code, which is okay. 必须在执行此代码之前执行这种str分配,这没关系。 Quickfix: 快速解决:

del str

Proper fix: find where this variable is defined and rename / delete it. 正确的解决方法:找到该变量的定义位置,然后重命名/删除它。

(note that input or float could be the names that have been redefined) (请注意, inputfloat可能是已重新定义的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM