簡體   English   中英

Python幫助:'str'對象不可調用

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

我正在學習Python,並在我的代碼中遇到此錯誤。 在線搜索,但找不到我理解的答案。 有人可以幫忙嗎?

TypeError:“ 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可能是已重新定義的名稱)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM