简体   繁体   English

python错误“ typeerror:nonetype对象不可调用”

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

I'm trying to make a simple timer script in python. 我正在尝试在python中创建一个简单的计时器脚本。 I see a lot of other people have had this error too, but I think my error may be different because I'm new to python: 我看到很多其他人也有这个错误,但是我认为我的错误可能有所不同,因为我是python新手:

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

below is the result: 结果如下:

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

Can anyone spot this error? 谁能发现这个错误? using %s has also failed me along with using +'s and str() around the time variable 使用%s也在时间变量周围使用+和str()失败了

A function can have only a single set of arguments. 一个函数只能有一组参数。

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

The above code should work. 上面的代码应该工作。

You are using the wrong syntax for the print function. 您为print功能使用了错误的语法。 Please note that print in is a callable. 请注意, 中的print是可调用的。 So when you call it, you can pass all that you need to be printed as parameters. 因此,当您调用它时,您可以将所有需要打印的内容作为参数传递。

Hence 因此

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

is the correct syntax. 是正确的语法。 You can optionally also specify a separator. 您还可以选择指定分隔符。


For posterity, TypeError: 'NoneType' object is not callable is an error thrown when you try to use an NoneType object as a callable . 为了后代,当您尝试使用NoneType 对象作为callable时, TypeError: 'NoneType' object is not callable 调用 Python flagged out when it discovered that you had tried to pass time (an object ) to print ("you have") which returns a NoneType object . 当Python发现您试图time (一个对象 )进行print ("you have")NoneTypeNoneType ,该操作返回了NoneType 对象

Remember, in print is a callable and it essentially returns a NullType object (which is nothing at all for that matter, and hence not callable ). 请记住,在 print可调用的 ,它本质上返回一个NullType 对象 (就此而言,什么都不是,因此是不可调用的 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM