Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm trying to pass two arguments to the switcher function, then use one of those arguments to define the switch case, and one of them to be passed to the case function. The portion of the code I provided has the function for one of the cases, the rest are similarly formatted.

I'm not sure if this is the right approach to creating this function, or why I am getting this error.

right = 1

def Default():

    print('Something went wrong :(')

def Right(duration):

    pyautogui.moveTo(1123,899) #Right Arrow Button

    pyautogui.mouseDown()

    time.sleep(duration)

    pyautogui.mouseUp()

    time.sleep(0.03)

def Move(direction,duration):

    switcher = {

        1: Right(duration) ,

        2: Left ,

        3: Down ,

        4: DiagonalUp 

        }

    return switcher.get(direction, Default)()

x = 2.5

Move(right,x)

The error I get is: line 48, in Move return switcher.get(direction, Default)() TypeError: 'NoneType' object is not callable

I do not get this error when I don't include the duration variable at all in the code.

1 Answer

0 votes
by (25.1k points)

A function by default in python returns None. So in your code when you call 

return switcher.get(direction, Default)()

It actually ends up calling it liike this:

return switcher.get(direction, None)()

Since the switcher dictionary has no key named None it returns None and then it gets being called as a function, which is what raising the exception. 

To resolve this issue you need Default to actually return a key present in switcher.

Related questions

0 votes
0 answers
asked Jun 24, 2021 in Python by priyanka (120 points)
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...