Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error with variables 3.8
#1
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
Reply
#2
please post the full traceback, in error tags, not just the last line
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I want to multiply two variables, but I get an error LeqendFire 3 2,088 Aug-09-2020, 03:52 PM
Last Post: LeqendFire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020