Python Forum

Full Version: error with variables 3.8
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a program that im working on to emulate a number of clicks over a user defined amount of time with a user defined delay seperation. im working on error checking for the variable type
heres the relevant code
def get_time():
    print("enter 1 for seconds")
    print("enter 2 for minutes")
    print("enter 3 for hours")
    timeTyp = input("please enter one of the coresponding numbers> ")
    timeLen = input("please enter how long you want to run> ")
    timeDel = input("please enter how long you want the delay between clicks in miliseconds or d for default> ")
    try:
        timeTyp = int(timeTyp)
        timeLen = int(timeLen)
        try:
            timeDel = float(timeDel*1000)
        except ValueError:
            if(timeDel.lower().strip(" ") == "d"):
                td = 1.0
            else:
                err = True
        if(timeTyp == 1):
            tl = timeTyp
        elif(timeTyp == 2):
            tl = timeTyp * 60
        elif(timeTyp == 3):
            tl = timeTyp * 3600
        if(err == True):
            raise ValueError
    except ValueError:
        print("invalid input. try again")
        tl, td = get_time()
    return tl, td
what am i doing wrong
i get an error of UnboundLocalError

also i forgot in global scope
err = False
please post the full traceback, in error tags, not just the last line
full code just for the heck of it
#afktool_v0.0.2
#include lib
import multiprocessing
import time as t
from pynput.mouse import Button, Controller
#init the mouse emulator
mouse = Controller()
#program info print
print("please note this will repetedly click the mouse. anywhere you have the mouse will be clicked 1 time a second")
print("afkTool version is in beta 0.0.2")
print("this tool is made to afk farm on a exp. mob farm in minecraft")
print("if you encounter an error that doesnt stop the program /n press cntl and c on this window and contact the devs")
print("beta testers:")
print("none yet. why is this here?")
#global var def
stopTread = False
err = False
#function to read user input for when running
def main_u_input():
    input("press any key to either abort or continue once completed")
    stopThread = True
#main function
def main_thread(tSet, delaySet=1):
    for i in range(0,tSet):
        iterationsLeft = tSet - int(i)
        print("time left in seconds {}. press ctrl+c on this window to quit".format(iterationsLeft))
        mouse.click(Button.left,1)
        if(stopThread == True):
            print("aborting")
            break
        else:
            t.sleep(delaySet)
#function to get user input on wheathe to continue
def get_mode():
    print("pick one of the following:")
    print("0 = quit")
    print("1 = run")
    m = input("please type the corresponding number and press enter>_")
    try:
        m = int(m)
    except ValueError:
        print("invalid input. try again")
        m = getMode()
    return m
#function to get user input for time needs
def get_time():
    print("enter 1 for seconds")
    print("enter 2 for minutes")
    print("enter 3 for hours")
    timeTyp = input("please enter one of the coresponding numbers> ")
    timeLen = input("please enter how long you want to run> ")
    timeDel = input("please enter how long you want the delay between clicks in miliseconds or d for default> ")
    try:
        timeTyp = int(timeTyp)
        timeLen = int(timeLen)
        try:
            timeDel = float(timeDel*1000)
        except ValueError:
            if(timeDel.lower().strip(" ") == "d"):
                td = 1.0
            else:
                err = True
        if(timeTyp == 1):
            tl = timeTyp
        elif(timeTyp == 2):
            tl = timeTyp * 60
        elif(timeTyp == 3):
            tl = timeTyp * 3600
        if(err == True):
            raise ValueError
    except ValueError:
        print("invalid input. try again")
        tl, td = get_time()
    return tl, td
#main logic
if(__name__ == "__main__"):
    while(True):
        mode = get_mode()
        if(mode == 1):
            t, d = get_time()
            p = multiprocessing.Process(target=main_thread, args=(t, d))
            p.start()
            main_u_input()
            p.join()
        elif(mode == 0):
            break
        else:
            print("invalid input try again")
            continue
    quit()
and the error output
Traceback (most recent call last):
File "C:\Users\Owner\Documents\PythonScripts\AfkTool\afkToolv0_0_2.py", line 80, in <module>
t, d = get_time()
File "C:\Users\Owner\Documents\PythonScripts\AfkTool\afkToolv0_0_2.py", line 69, in get_time
if(err == True):
UnboundLocalError: local variable 'err' referenced before assignment
on line 62 you assign to err. Thus err in the body of get_time() is different variable (i.e. with local scope) than the err in the global scope. obviously it's not the else part that is executed and on line 69 err is not defined.
to resolve it you can add global err statement at the top of get_time() function.
However note that using global variables is bad practice. So the best would be to refactor your code.