UnboundLocalError :(
Verfasst: Sonntag 19. April 2020, 17:05
Hallo,
Ich programmiere jetzt seit ca 1 und einer halben Woche mit Python und das Problem ist, dass ich die ganze Zeit einen Fehler (UnboundLocalError).
Ich weiß so ziemlich worum es sich bei dem Fehler handelt, jedoch passt das nicht bei mir ins Script rein, denn ich erstelle die Variable ganz am Anfang des Codes. Es handelt sich um die Variable indexOfBack, die einen Fehler wirft, sobald man sie mit Back() versucht abzufragen
Error:
UnboundLocalError Traceback (most recent call last)
<ipython-input-32-70f170e79fd9> in <module>
50
51 if command == "Key.cmd":
---> 52 back()
53 Other(command)
54 win = True
<ipython-input-32-70f170e79fd9> in back()
45
46 def back():
---> 47 if indexOfBack > 0:
48 txt.write("\n[Backspace x" + str(indexOfBack) + "]\n")
49 indexOfBack = 0
UnboundLocalError: local variable 'indexOfBack' referenced before assignment
Ich programmiere jetzt seit ca 1 und einer halben Woche mit Python und das Problem ist, dass ich die ganze Zeit einen Fehler (UnboundLocalError).
Ich weiß so ziemlich worum es sich bei dem Fehler handelt, jedoch passt das nicht bei mir ins Script rein, denn ich erstelle die Variable ganz am Anfang des Codes. Es handelt sich um die Variable indexOfBack, die einen Fehler wirft, sobald man sie mit Back() versucht abzufragen
Error:
UnboundLocalError Traceback (most recent call last)
<ipython-input-32-70f170e79fd9> in <module>
50
51 if command == "Key.cmd":
---> 52 back()
53 Other(command)
54 win = True
<ipython-input-32-70f170e79fd9> in back()
45
46 def back():
---> 47 if indexOfBack > 0:
48 txt.write("\n[Backspace x" + str(indexOfBack) + "]\n")
49 indexOfBack = 0
UnboundLocalError: local variable 'indexOfBack' referenced before assignment
Code: Alles auswählen
#CAPS Lock wird nicht beachtet
caps = False
strg = False
alt = False
win = False
backspace = False
indexOfBack = 0
with open("./log.txt","r") as file:
for line in file:
line = line.strip()
line = line.split(";")
if len(line) == 2:
command = line[1]
time = line[0]
command = command.replace("'","")
command = command.strip()
time = time.replace("[","")
time = time.replace("]","")
time = time.split(":")
time = time[1]
with open("./log_Auswertung.txt", "a+") as txt:
def Other(key):
if strg == True and str(key) != "Key.ctrl_l" or strg == True and str(key) != "Key.ctrl_r" or alt == True and str(key) != "Key.alt_l" or alt == True and str(key) != "Key.alt_r" or win == True and str(key) != "Key.cmd":
if str(key) == "Key.cmd":
txt.write(" + GUI")
elif str(key) == "Key.ctrl_r" or str(key) == "Key.ctrl_l":
txt.write(" + STRG")
elif str(key) == "Key.alt_r" or str(key) == "Key.alt_l":
txt.write(" + ALT")
elif strg == False and alt == False and win == False:
if str(key) == "Key.cmd":
txt.write("[GUI")
elif str(key) == "Key.ctrl_r" or str(key) == "Key.ctrl_l":
txt.write("[STRG")
elif str(key) == "Key.alt_r" or str(key) == "Key.alt_l":
txt.write("[ALT")
def back():
if indexOfBack > 0:
txt.write("\n[Backspace x" + str(indexOfBack) + "]\n")
indexOfBack = 0
if command == "Key.cmd":
back()
Other(command)
win = True
elif command == "Key.ctrl_l" or command == "Key.ctrl_r":
back()
Other(command)
strg = True
elif command == "Key.alt_l" or command == "Key.alt_r":
back()
Other(command)
alt = True
elif command == "Key.tab":
back()
if strg == False and alt == False and win == False:
txt.write("\n[TAB]\n")
elif command == "Key.enter":
back()
txt.write("\n[ENTER]\n")
elif command == "Key.caps_lock":
if caps == True:
caps = False
else:
caps = True
elif command == "Key.backspace":
indexOfBack += 1
elif command == "Key.space":
back()
txt.write(" ")
elif command == "Key.esc":
back()
if strg == False and alt == False and win == False:
txt.write("\n[ESC]\n")
else:
txt.write(" + ESC")
else:
back()
if command == "Key.shift":
indexOfBack = 0
elif command == "\x01":
txt.write(" + A]")
strg = False
alt = False
win = False
elif command == "\x13":
txt.write(" + S]")
strg = False
alt = False
win = False
elif command == "\x04":
txt.write(" + D]")
strg = False
alt = False
win = False
elif command == "\x06":
txt.write(" + F]")
strg = False
alt = False
win = False
elif command == "\x19":
txt.write(" + Y]")
strg = False
alt = False
win = False
elif command == "\x18":
txt.write(" + X]")
strg = False
alt = False
win = False
elif command == "\x03":
txt.write(" + C]")
strg = False
alt = False
win = False
elif command == "\x16":
txt.write(" + V]")
strg = False
alt = False
win = False
elif command == "\x0e":
txt.write(" + N]")
strg = False
alt = False
win = False
elif command == "\x1a":
txt.write(" + Z]")
strg = False
alt = False
win = False
elif command == "\x0f":
txt.write(" + O]")
strg = False
alt = False
win = False
elif command == "\x10":
txt.write(" + P]")
strg = False
alt = False
win = False
else:
char = "abcdefghijklmnopqrstuvwxyzüäöABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜ<>|,;.:-_#'+*~´`?ß}=])[({/&%$§^°@€"
found = False
for letter in char:
if command == letter:
found = True
if caps == True:
txt.write(command.upper())
else:
txt.write(command)
if found == False:
txt.write("\n(" + command + ")\n")
print("Finished")