Why isn't my python function being defined?

I have been using python for a while know but have never had this issue before. Whenever I go to create a function in my python file it doesn’t give me any errors, but when I go to use the function it tells me its not defined.

Heres the code:

import pyautogui
import keyboard
import time

while True:
        if keyboard.is_pressed('`'):

def macro(type,key,seconds):
    endtime = time.time() + seconds
    while time.time() < endtime:
        if type == 'hold':

The macro('hold','w',3) on line 8 is where the error "macro" is not defined is.

I’ve checked several different post but they are all different to this.

>Solution :

The first block of code is being executed before the function is defined in the second block.

The common way to deal with this is to put your main block of code (while ...) inside a def main(): function, then call it at the end of the file.

But you can also just move everything down if you want.

Leave a Reply