Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting 'Nonetype is not callable' error but I don't know why

I’m trying to create a reaction time game thing, however when I press enter after the hotkey is added I get a super long Nonetype error making reference to several different files.

import time
import datetime
import random
import keyboard

playing=True

def stop(playing):
    playing=False

input("Press enter when you're ready to test your reaction time!")
print("Press enter when you see the cat!")

time.sleep(1)

keyboard.add_hotkey('enter', stop(playing))

time.sleep(random.randint(3,6))

if(playing==True):
    keyboard.remove_hotkey('enter')
    start = datetime.datetime.now()
    input("Press Enter!!!!")
    end = datetime.datetime.now()
    print(end-start)

The idea of the hotkey is to stop the user from being able to just hold the enter button down. I don’t know why this doesn’t work.

I tried adding ‘return playing’ to the end of the function but that just gave me a similar error of ‘bool is not callable’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

stop(playing) returns None, so when you do:

keyboard.add_hotkey('enter', stop(playing))

it’s exactly as if you did:

stop(playing)
keyboard.add_hotkey('enter', None)

The second argument to add_hotkey is supposed to be a function (not the result of calling a function), so if you pass it None, it raises an error.

What I think you want to do is define your stop function so that it can be called with no arguments, and sets the global playing variable to False:

def stop()
    global playing
    playing = False

and then pass the function (without calling it yet) to add_hotkey:

keyboard.add_hotkey('enter', stop)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading