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