![](/img/trans.png)
[英]Python Error: TypeError: 'NoneType' object is not callable
[英]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
功能使用了錯誤的語法。 請注意, python-3中的print
是可調用的。 因此,當您調用它時,您可以將所有需要打印的內容作為參數傳遞。
因此
print ("you have", time, "seconds left")
是正確的語法。 您還可以選擇指定分隔符。
為了后代,當您嘗試使用NoneType
對象作為callable時, TypeError: 'NoneType' object is not callable
調用 。 當Python發現您試圖time
(一個對象 )進行print ("you have")
, NoneType
了NoneType
,該操作返回了NoneType
對象 。
請記住,在python-3中, print
是可調用的 ,它本質上返回一個NullType
對象 (就此而言,什么都不是,因此是不可調用的 )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.