Fixing Input UnboundLocalError
Question:
In my code I’m trying to code a password login loop that will keep requiring a password input until a correct password (defined in an .env file) is given. However, when I run the code below I get an "UnboundLocalError cannot access local variable ‘input’ where it is not associated with a value."
Any ideas?
Here is my code that gives the error:
`from decouple import config
from speech_rec import speak, take_user_input
LOGIN=config("SHA-512")
def login():
while True:
input=input("PASSWORD: ")
if input==LOGIN:
break
else:
speak("Try Again")
continue
login()
#rest of code
for i in range(10):
print(i)`
Answers:
This is most likely because you used one of python’s built-in (input
) as a variable name.
import builtins
print("input" in dir(builtins))
#True
Try to give your variable a different name :
from speech_rec import speak, take_user_input
LOGIN=config("SHA-512")
def login():
while True:
input_val = input("PASSWORD: ")
if input_val == LOGIN:
break
else:
speak("Try Again")
continue
In my code I’m trying to code a password login loop that will keep requiring a password input until a correct password (defined in an .env file) is given. However, when I run the code below I get an "UnboundLocalError cannot access local variable ‘input’ where it is not associated with a value."
Any ideas?
Here is my code that gives the error:
`from decouple import config
from speech_rec import speak, take_user_input
LOGIN=config("SHA-512")
def login():
while True:
input=input("PASSWORD: ")
if input==LOGIN:
break
else:
speak("Try Again")
continue
login()
#rest of code
for i in range(10):
print(i)`
This is most likely because you used one of python’s built-in (input
) as a variable name.
import builtins
print("input" in dir(builtins))
#True
Try to give your variable a different name :
from speech_rec import speak, take_user_input
LOGIN=config("SHA-512")
def login():
while True:
input_val = input("PASSWORD: ")
if input_val == LOGIN:
break
else:
speak("Try Again")
continue