簡體   English   中英

python錯誤“ typeerror:nonetype對象不可調用”

[英]python error “typeerror: nonetype object not callable”

我正在嘗試在python中創建一個簡單的計時器腳本。 我看到很多其他人也有這個錯誤,但是我認為我的錯誤可能有所不同,因為我是python新手:

time=60
from time import sleep
while (time>0):
    print ("you have") (time) ("seconds left")
    time-=1
    sleep(1)

結果如下:

>>> you have Traceback (most recent call last): File "H:\\counter.py", line 4, in <module> print ("you have") (time) ("seconds left") TypeError: 'NoneType' object is not callable

誰能發現這個錯誤? 使用%s也在時間變量周圍使用+和str()失敗了

一個函數只能有一組參數。

print_statement = "you have" + str(time) + "seconds left"
print(print_statement)

上面的代碼應該工作。

您為print功能使用了錯誤的語法。 請注意, 中的print是可調用的。 因此,當您調用它時,您可以將所有需要打印的內容作為參數傳遞。

因此

print ("you have", time, "seconds left")

是正確的語法。 您還可以選擇指定分隔符。


為了后代,當您嘗試使用NoneType 對象作為callable時, TypeError: 'NoneType' object is not callable 調用 當Python發現您試圖time (一個對象 )進行print ("you have")NoneTypeNoneType ,該操作返回了NoneType 對象

請記住,在 print可調用的 ,它本質上返回一個NullType 對象 (就此而言,什么都不是,因此是不可調用的 )。

暫無
暫無

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

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