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

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:
    try:
        if keyboard.is_pressed('`'):
            macro('hold','w',3)
            break
    except:
        break

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

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

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

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.

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