0

I'm trying to make a simple timer script in 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:

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@dioretsa: Please accept my answer if it had helped you :)
2

You are using the wrong syntax for the print function. Please note that print in is a callable. 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. Python flagged out when it discovered that you had tried to pass time (an object) to print ("you have") which returns a NoneType object.

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

2 Comments

Even for python2, the syntax is wrong and should be print ("you have"), (time), ("seconds left"). dioretsa's print almost looks like haskell function calls
@Lærne True that. :D. It is also an excellent example of tuple packing and unpacking in python-2 print. I only wrote in reference to python-3 because that's what the original code language looked like to me. python-2 print doesn't really require individual parameters to be put inside parentheses (seems excessive and promotes bad practices).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.