python error "typeerror: nonetype object 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 函数使用了错误的语法。请注意 中的 print 是可调用的。所以当你调用它的时候,你可以将所有你需要打印的作为参数传递。

因此

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

是正确的语法。您还可以选择指定分隔符。


为了后代,TypeError: 'NoneType' object is not callable 是当您尝试使用 NoneType object 作为 callable[=40 时抛出的错误=].当 Python 发现您试图将 time(一个 对象 )传递给 print ("you have") 时,Python 被标记出来,其中 returns 一个 NoneType 对象

记住,在 print 中是一个 callable 并且它本质上是 returns 一个 NullType object(就此而言,它什么都不是,因此 不可调用)。